Merge Overlapping Intervals
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Goldman Sachs reportedly put Merge Overlapping Intervals in front of candidates in September 2026, and the input size is the first thing to read. Up to 100000 intervals means comparing every pair is dead on arrival. That's roughly five billion comparisons. The fix is a sort and a single pass, and it's a classic array problem you can finish fast if you stay calm. If your mind goes blank when the timer starts, StealthCoder is a desktop overlay that stays invisible during screen share and can hand you the solution live. Still, this one is very learnable tonight. Here's the pattern and the trap.
The problem
Given a list of closed integer intervals, merge every pair that overlaps and return the disjoint merged intervals ordered by start coordinate. Closed intervals that share an endpoint overlap, so [1,4] and [4,5] merge into [1,5]. Function mergeIntervals(intervals: int[][]) → int[][] Examples Example 1 intervals = [[1,3],[2,6],[8,10],[15,18]] return = [[1,6],[8,10],[15,18]] The first two intervals overlap and merge. Example 2 intervals = [[1,4],[4,5]] return = [[1,5]] Closed intervals sharing endpoint 4 overlap. Constraints 0 <= intervals.length <= 100000. Every interval has exactly two values and -10^9 <= start <= end <= 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Sort the intervals by start. Then walk them once, keeping a current merged interval. If the next start is less than or equal to the current end, extend the end with max(currentEnd, nextEnd). Otherwise push the current one and start a new one. That's O(n log n) time, dominated by the sort. The trap is the comparison. The problem says closed intervals, so [1,4] and [4,5] merge, which means you use <= and not <. The second trap is taking the next end blindly instead of the max, which breaks on [1,10],[2,3]. Also handle the empty input, since length can be 0. Values reach 10^9 in magnitude, so don't build the sort key with arithmetic that could overflow in a fixed-width language. If you blank on the live OA, StealthCoder is your hedge, but this pattern is short enough to write from memory.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Merge Overlapping Intervals 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as merge intervals. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Goldman Sachs's OA.
Goldman Sachs reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Merge Overlapping Intervals FAQ
What's the trick to Merge Overlapping Intervals?+
Sort by start, then sweep once. Keep a running interval and compare each next start to its end. If start <= end, merge by taking the max of the two ends. Otherwise close the current interval and begin a new one. That's the whole solution.
How hard is this really for the Goldman Sachs OA?+
It's a medium on paper but a well-worn one. The logic is about ten lines once you see sort plus sweep. Difficulty comes from edge cases: touching endpoints, nested intervals, and empty input. Candidates who've seen it once usually finish quickly.
Why does [1,4] and [4,5] merge here?+
The problem defines intervals as closed, so shared endpoints count as overlap. In code that means your check is next.start <= current.end. Using a strict less-than is the most common way to fail the second example, which expects [[1,5]].
What time complexity should I aim for?+
O(n log n) from sorting, plus O(n) for the sweep, with n up to 100000. Anything quadratic will be too slow. Extra space is O(n) for the output, or O(1) beyond that if you merge in place after sorting.
How do I prepare for this in 48 hours?+
Write the solution from scratch three times without looking. Then test it on nested intervals like [1,10],[2,3], touching ones like [1,4],[4,5], a single interval, and an empty list. Those four cases cover nearly every bug people hit.