Reported July 2026
Hudson River Tradingsorting

Cumulative Unique Bytes

Reported by candidates from Hudson River Trading's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Hudson River Trading OA. Under 2s to a working solution.
Founder's read

The edge case that kills the naive solution here is the byte range itself. Hudson River Trading reported this one in July 2026, and endpoints go up to 10^12, so any approach that marks individual bytes in an array or set blows up before it returns. Cumulative Unique Bytes is an interval merging problem in disguise. You get up to 1000 segments, each one overlapping, duplicating, or bridging earlier ones, and you report the running union size after each. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you a working solution in real time.

The problem

A team of astronauts is receiving a file from Earth in a sequence of data segments. Each segments[i] contains two 64-bit integers: the 1-based index of the first byte and the 1-based index of the last byte in that segment. Both endpoints are inclusive.
After each segment is received, calculate the total number of unique byte indices covered by all segments received so far. Segments may overlap or completely duplicate earlier segments, and each byte must be counted only once.
Return an array where result[i] is the cumulative number of unique bytes received after processing segments[i].

Function
solution(segments: long[][]) → long[]

Examples
Example 1
segments = [[1,1],[2,2],[3,3]]
return = [1,2,3]
The first segment contributes byte 1. The second contributes byte 2, and the third contributes byte 3, so the cumulative totals are [1, 2, 3].
Example 2
segments = [[1,1],[2,2],[3,5]]
return = [1,2,5]
The first two segments cover bytes 1 and 2. The final segment adds bytes 3 through 5, increasing the cumulative total to 5.
Example 3
segments = [[1,9],[1,3],[8,15],[6,9],[2,5]]
return = [9,9,15,15,15]
The first segment covers bytes 1 through 9. The second segment is already covered. The third extends the covered range through byte 15, and the remaining segments add no new bytes.
Example 4
segments = [[7,9],[1,3],[8,15],[6,9],[2,4]]
return = [3,6,12,13,14]
The cumulative covered intervals evolve from [7, 9], to [1, 3] and [7, 9], then to [1, 3] and [7, 15]. The fourth segment adds byte 6, and the fifth adds byte 4, producing totals [3, 6, 12, 13, 14].

Constraints
1 <= segments.length <= 1000
segments[i].length = 2
1 <= segments[i][0] <= segments[i][1] <= 10^12

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to never touch individual bytes. Keep a sorted list of disjoint covered intervals and a running total. For each new segment, find every stored interval that overlaps or touches it, remove them, and merge them into one interval with min start and max end. Subtract the removed lengths from the total, add the merged length, and append the total to the result. With 1000 segments, a plain list scan per insert is O(n^2), which is fine. The pitfalls are off-by-one on inclusive endpoints (length is end - start + 1) and the adjacency question. Example 4 shows [1,3] and [4,4] covering 4 bytes, so merging adjacent intervals is optional for counting but harmless. Use 64-bit integers. If the interval logic tangles under pressure, StealthCoder is the hedge on the live OA, since it reads the problem and hands you the merge loop.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Cumulative Unique Bytes 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Hudson River Trading's OA.

Hudson River Trading reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Cumulative Unique Bytes FAQ

What's the trick in Cumulative Unique Bytes?+

Don't track bytes, track disjoint intervals. Keep a sorted list of merged ranges plus a running total. Each new segment absorbs every overlapping range, you adjust the total by the difference in covered length, and you record it. Endpoints reach 10^12, so per-byte storage is impossible.

How hard is this one really?+

Medium. The idea is standard interval merging, but the incremental part and inclusive endpoints trip people up. With only 1000 segments, an O(n^2) scan is fast enough, so you don't need a balanced tree or anything fancy.

Do I need a fancy data structure?+

No. A plain sorted array or list of intervals works because n is at most 1000. A sorted map with binary search is cleaner for larger inputs, but it's overkill here. Simple and correct beats clever in an assessment.

What edge cases should I test?+

Test a fully duplicate segment, a segment that swallows several existing intervals, single-byte segments like [5,5], and adjacent ranges like [1,3] then [4,6]. Also use values near 10^12 to confirm you're using 64-bit integers and not overflowing.

How do I prepare in 48 hours?+

Solve the classic merge intervals and insert interval problems until the overlap condition is automatic. Then write the incremental version, tracking a running total. Practice the length formula end - start + 1 so inclusive bounds don't cost you a wrong answer.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Hudson River Trading.

OA at Hudson River Trading?
Invisible during screen share
Get it