interviewpath
  • 0day streak
  • 10total XP
  • Level 1
0/6

Unit 1 · Algorithms · Lesson 8 of 24

Prefix sums, intervals and sweep lines

Prefix sums answer range questions in constant time after one linear pass, difference arrays apply range updates cheaply, and sorted interval and event processing handles overlaps, merges and peak load. The same patterns appear in scheduling, rate limiting and time-series code.
About 4 h 30 minFocus mode

Step 1: Learn the ideas

  • Prefix sums with a leading zero; range sums as differences of prefixes
  • Prefix sums plus hash maps for subarray counting
  • Prefix sums modulo k and first-seen indices
  • Prefix and suffix products
  • 2-D prefix sums and inclusion-exclusion
  • Difference arrays for range updates
  • Interval merging, insertion and intersection
  • Sweep line over sorted events and tie-breaking
  • Earliest-end-first interval selection
  • Fenwick and segment trees for interleaved updates and queries

Why it matters

Range questions are everywhere: requests in a time window, sessions that overlap, bookings that conflict, load per server after many range updates. Recomputing each answer from scratch costs O(n) per query. Prefix sums, difference arrays and sweep lines move that work into one preprocessing pass or one sorted scan, which is exactly the transformation interviewers hope you find.

Core ideas

Pattern: Prefix sums. Define P[0] = 0 and P[i + 1] = P[i] + a[i]. The sum of a[l..r] (inclusive) is P[r + 1] − P[l]. The leading zero removes the special case for ranges that start at index 0. Building P costs O(n) and each query O(1). The idea extends to counts and XOR, and to products through prefix and suffix arrays (Product of Array Except Self). In two dimensions, S[i][j] holds the sum of the rectangle above and to the left, and any sub-rectangle sum takes four lookups by inclusion and exclusion.

Prefix sums with a hash map. A subarray a[l..r] sums to k exactly when P[r + 1] − P[l] = k. Scan left to right with a map from each prefix value to how often it has occurred; at each step add count[P − k] to the answer, then record P. Seed the map with {0: 1} so subarrays starting at index 0 are counted. This works with negative numbers, where sliding windows fail. Variants store the first index of each prefix (longest subarray with a property) or prefixes modulo k (Continuous Subarray Sum); in languages where % can be negative, normalize the remainder.

Pattern: Sweep line / difference arrays. To add v to every position in [l, r], do D[l] += v and D[r + 1] −= v; after all updates, one prefix pass over D yields the final values. k updates over n positions cost O(n + k) instead of O(n × k) (Car Pooling, Corporate Flight Bookings). When coordinates are large or sparse, turn each interval into two events, (start, +1) and (end, −1), sort them and sweep while tracking the running total; its maximum is the peak number of simultaneous intervals. The tie order encodes your boundary convention: for half-open intervals [start, end), process ends before starts at equal coordinates so that touching intervals do not count as overlapping.

Pattern: Interval merging. Sort by start and keep a current merged interval. If the next interval starts at or before the current end, extend the end to the larger of the two ends; otherwise emit the current interval and start a new one. That is O(n log n) for the sort plus a linear pass. Insert Interval on already sorted input is linear: copy intervals that end before the new one, merge the overlapping ones, copy the rest. To intersect two sorted interval lists, use two pointers and advance whichever interval ends first.

Selection. Keeping the maximum number of non-overlapping intervals is a greedy choice: sort by end and keep each interval that starts after the last kept end. The proof is an exchange argument, covered in the greedy topic.

Limits. Static prefix sums assume no updates. When updates and queries interleave, use a Fenwick (binary indexed) tree or a segment tree with O(log n) per operation; for a dynamic set of intervals with overlap queries, an ordered map keyed by start works.

Common misconceptions

  • "Prefix sums need non-negative numbers." They work for any values; it is the sliding window that needs non-negative values.
  • "The leading zero is optional." Without P[0] = 0, or {0: 1} in the map, subarrays that start at index 0 are missed.
  • "Touching intervals overlap." That depends on whether intervals are closed or half-open; decide, say it, and make comparisons and event tie-breaking match.
  • "Sorting by start or by end is interchangeable." Merging sorts by start; selecting the most non-overlapping intervals sorts by end.
  • "A difference array answers queries during updates." Values exist only after the prefix pass; interleaved queries need a Fenwick tree or a segment tree.

How to approach it

Ask whether intervals are closed or half-open, whether the input is sorted, and whether updates interleave with queries. Write the prefix definition with explicit indices and check it on a three-element example before coding. For sweeps, write the event tuple and tie-breaking rule down first. Interviewers probe boundary conventions, negative numbers and what changes when the data arrives as a stream rather than a batch.

Suggested order

  1. 1Read Chapter 9.1 (Static array queries) of the Competitive Programmer’s Handbook for one- and two-dimensional prefix sums, then the range-updates part of 9.4 for difference arrays.
  2. 2Solve Range Sum Query 2D - Immutable using inclusion-exclusion, and Product of Array Except Self with prefix and suffix products in O(1) extra space.
  3. 3Solve Subarray Sum Equals K with a prefix-count map, then Continuous Subarray Sum with prefixes modulo k.
  4. 4Solve Merge Intervals and Insert Interval after writing down your closed or half-open convention.
  5. 5Solve Non-overlapping Intervals with the earliest-end greedy and Interval List Intersections with two pointers.
  6. 6Solve Car Pooling twice, once with a difference array and once with sorted events, then Corporate Flight Bookings.
  7. 7For deep-level preparation, read Chapter 9.2 (Binary indexed tree) of the same handbook and implement point update with prefix query.

Step 2: Check off what you can do

Tap each one you can genuinely do or explain. Each earns 10 XP.

Step 3: Go deeper

  • Competitive Programmer’s Handbook: search, amortized-analysis, range-query and trie sectionsStart here

    CSES

    Section: Ch. 9: Range queries (9.1 Static array queries; range updates in 9.4)

    Prefix sums in one and two dimensions, the difference-array technique for range updates, and the Fenwick and segment trees needed once updates interleave with queries.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • LeetCode 75 study plan

    LeetCode

    Its prefix-sum and interval problems give a first practice pass after reading the notes.

    Open(opens in a new tab)Not checkable automatically, 5 Oct 2026
  • LeetCode Top Interview 150 study plan

    LeetCode

    Its interval problems are useful for review rotation with problems you have not seen.

    Open(opens in a new tab)Not checkable automatically, 5 Oct 2026

Step 4: Practice

Write the prefix definition (with the leading zero) or the interval convention (closed or half-open) before each problem. For Subarray Sum Equals K and Continuous Subarray Sum, say what the map stores and why it is seeded. Solve Car Pooling with both a difference array and a sorted-event sweep and compare when each is preferable; Corporate Flight Bookings is the pure difference-array check.

Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).

Step 5: Test yourself

Answer each one out loud or on paper before you finish the lesson.

  1. Question 1Why does the prefix array start with 0, and what breaks without it?
  2. Question 2What does the map store in prefix-sum subarray counting, and how is it seeded?
  3. Question 3How do you order start and end events that share a coordinate for half-open intervals?
  4. Question 4Why does merging sort by start while maximum non-overlapping selection sorts by end?
  5. Question 5What does a difference array cost for k updates over n positions?
  6. Question 6What changes if queries arrive between updates?

You’re done when you can

  • Solve Subarray Sum Equals K and explain why a sliding window would fail on it, in under 20 minutes.
  • Merge intervals and compute peak overlap correctly for both closed and half-open conventions.
  • Apply k range updates with a difference array and explain its cost compared with direct updates.
  • Explain when you would need a Fenwick or segment tree instead of static prefix sums.

Finish the lesson

How confident do you feel?

Lesson complete!

+50 XP

Reviews will come up on the Practice tab so it sticks.

Your notes and study time

Log study time