Merge Overlapping Intervals
Reported by candidates from Capgemini's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Capgemini OA reported in September 2026 hides its trap in one line: closed intervals that share an endpoint merge. [1,4] and [4,5] become [1,5]. A naive solution that checks for strict overlap fails that case and you don't find out until the hidden tests run. This is the classic merge intervals problem, an array and sorting task, and it's very doable in an evening. Sort by start, sweep once, extend or push. If you blank on the sweep logic during the live OA, StealthCoder runs invisibly as a safety net and gives you the solution on screen.
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
The trick is sorting by start coordinate first. After that, one pass does everything. Keep a result list. For each interval, if its start is less than or equal to the last merged end, set that end to the max of the two ends. Otherwise push it as a new interval. The pitfalls are all small. Use <= and not <, because the problem says shared endpoints overlap. Use max for the end, because [1,10] followed by [2,3] must stay [1,10]. Handle the empty input, since length can be 0. Sorting dominates, so it's O(n log n) with n up to 100000, which is fine. Values reach 10^9 in magnitude, but you only compare them, so overflow isn't a concern. Don't mutate the input in a way that breaks your sort comparator. If the comparator or the edge cases slip away under pressure, StealthCoder is the hedge on the live OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
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. 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
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 Capgemini's OA.
Capgemini 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.
Merge Overlapping Intervals FAQ
What's the trick in the Capgemini merge intervals question?+
Sort by start, then sweep once. Compare each interval's start to the current merged end. If start <= end, merge by taking the max end. Otherwise start a new interval. The detail that catches people is that touching endpoints count as overlap.
How hard is this OA problem really?+
It's a medium-level problem with a well-known solution. The logic is short, around 10 lines. Difficulty comes from edge cases: empty input, touching endpoints, and nested intervals. If you've seen merge intervals once, you can finish it quickly.
Which edge cases should I test before submitting?+
Test an empty list, a single interval, [[1,4],[4,5]] for the shared endpoint, a nested case like [[1,10],[2,3]], and unsorted input. Also try negative values, since starts can go down to -10^9. These cover most hidden test failures.
What's the time and space complexity?+
Time is O(n log n) because of the sort, and the sweep is O(n). Space is O(n) for the output list, plus whatever your sort uses. With n up to 100000, that's comfortably fast. A brute force pairwise approach would be too slow.
How do I prepare for this in 48 hours?+
Write the solution from scratch twice without looking. Then run it on the two examples plus the edge cases above. Focus on the <= comparison and the max on the end value. That's the whole problem, so you don't need broad prep beyond it.