Difference Arrays: The Range-Update Trick Every Coding Interview Candidate Should Know
8 views
Many interview problems ask you to apply lots of updates to ranges of an array, then report the final values: "add 5 to every element from index 2 to 7," repeated thousands of times. Doing each update element by element costs O(n) per update, which times out quickly. A difference array turns every range update into two O(1) operations, followed by one prefix-sum pass at the end.
THE IDEA
For an array A, define the difference array D where D[i] = A[i] - A[i-1] (and D[0] = A[0]). Adding a value v to every element in A[l..r] changes only two entries of D:
- D[l] += v (everything from l onward goes up),
- D[r+1] -= v (cancel the increase after r).
After all updates, a running prefix sum over D rebuilds A.
def apply_ranges(n, updates):
diff = [0] * (n + 1)
for l, r, v in updates:
diff[l] += v
diff[r + 1] -= v
out, run = [], 0
for i in range(n):
run += diff[i]
out.append(run)
return out
Total cost: O(n + q) for q updates, instead of O(n * q).
CLASSIC INTERVIEW PROBLEMS
1. Range addition. Exactly the template above.
2. Corporate flight bookings. Each booking reserves seats on flights first..last. Convert to zero-based indices and apply the template.
3. Car pooling. Trips add passengers at a pickup point and remove them at drop-off. Mark +passengers at the start and -passengers at the end, sweep, and check whether the running load ever exceeds capacity. Note that drop-off happens before the next pickup at the same point, so the end index is exclusive.
4. Meeting rooms / maximum overlap. Mark +1 at each start and -1 at each end, then sweep in order. The maximum running value is the number of rooms needed. When coordinates are large, sort the event points (a sweep line) instead of allocating a giant array.
5. Minimum number of increments or "zero array" problems. Some questions ask whether a set of range decrements can turn an array to zeros; difference arrays let you track how much decrement is active at each position while scanning greedily.
2D DIFFERENCE ARRAYS
To add v to a rectangle (r1, c1) to (r2, c2):
D[r1][c1] += v
D[r1][c2+1] -= v
D[r2+1][c1] -= v
D[r2+1][c2+1] += v
Then take 2D prefix sums. This is the inclusion-exclusion mirror of the 2D prefix-sum query, and interviewers like candidates who notice that symmetry.
COMMON MISTAKES
- Off-by-one at the end: allocate n + 1 slots so D[r+1] never goes out of bounds.
- Inclusive versus exclusive end points, especially in pickup/drop-off problems.
- Forgetting that the final sweep must happen after all updates. If queries are interleaved with updates, you need a Fenwick tree or segment tree instead, and saying so earns points.
HOW TO PRESENT IT
Start with the naive O(n * q) loop, point out that each update touches many elements, and then explain that you only need to record where an increase starts and stops. Draw a five-element example on the whiteboard with two overlapping updates and walk through the sweep. That concrete trace usually convinces the interviewer faster than any formula.
Explaining patterns like this calmly while someone watches is a skill on its own. Some candidates use Linkjob AI (https://linkjob.ai), which gives real-time help during live job interviews and technical interviews, to keep their reasoning organized. Once you recognize "many range updates, one final read," difference arrays will feel like a shortcut you can reach for every time.