Running Union Length after Interval Uploads
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this ZipRecruiter OA, reported in October 2022, is adding each new interval's raw length and forgetting the overlap. You upload [1,5] then [3,8] and the answer is 7, not 9. The task is a running union length over half-open intervals, with up to 2000 uploads and coordinates out to 10^12. It's an array problem with a merge twist, and it's very doable if you keep the interval list clean. If you blank mid-assessment, StealthCoder runs invisibly on your screen as a safety net and hands you the merge logic.
The problem
Each row [left, right] uploads a continuous half-open interval [left, right) with left < right. Intervals may overlap or touch. After every upload, return the total length covered by the union of all intervals seen so far. Function runningUnionLengths(uploads: long[][]) → long[] Examples Example 1 uploads = [[1,5],[3,8]] return = [4,7] The second interval adds only its uncovered suffix. Example 2 uploads = [[1,2],[5,9]] return = [1,5] Disjoint lengths add. Constraints 1 <= uploads.length <= 2000 -10^12 <= left < right <= 10^12
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to maintain a sorted list of disjoint intervals. For each upload, find every stored interval that overlaps or touches [l, r), remove those, and merge them into one interval using min of the lefts and max of the rights. Then adjust the running total: subtract the lengths you removed, add the length of the merged interval. With n at most 2000, a linear scan per upload gives O(n^2), which is fine. The pitfalls are touching intervals, since [1,2) and [2,5) should merge, off-by-one thinking on half-open ranges, and overflow. Use 64-bit integers, because coordinates reach 10^12 and the totals get bigger. Don't recompute the whole union from scratch with a sort each time unless you've checked the cost. StealthCoder is the hedge if the merge loop gets tangled live and you need a clean version fast.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Running Union Length after Interval Uploads cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Running Union Length after Interval Uploads FAQ
How hard is this ZipRecruiter OA problem really?+
Medium at most. The idea is simple: merge intervals and track the total. The difficulty is in the edge cases, like touching intervals and removing several overlapped intervals at once. With 2000 uploads, a quadratic approach passes, so you don't need a fancy structure.
What's the trick to getting the running union right?+
Keep a sorted list of disjoint intervals plus a running total. For each new interval, pull out everything that overlaps or touches it, subtract their lengths, merge into one interval, and add its length back. The answer after each upload is the total.
Do touching intervals like [1,2) and [2,5) merge?+
Yes, treat them as mergeable. Since the intervals are half-open, they cover [1,5) together with no gap. Whether you merge them or keep them separate, the total length is the same, but merging keeps the list tidy and avoids bugs.
Do I need a segment tree or balanced tree here?+
No. With at most 2000 uploads, an O(n^2) scan over a sorted list is plenty. A tree structure would give O(n log n) but adds code and risk. Pick the simple version unless you're very comfortable with the alternative.
How do I prepare for this in 48 hours?+
Write the merge-intervals routine from memory twice. Then extend it to track a running total and test with overlap, touching, nested, and disjoint cases. Use long integers for everything, since coordinates go up to 10^12 in magnitude.