Reported June 2024
Zscalersorting

Merge Intervals

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

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

Zscaler reported this one in June 2024, and the input size is the whole story. Up to 10000 intervals means comparing every pair is wasteful, and you don't need to. It's Merge Intervals, an array problem with one well-known move: sort by start, then sweep once. If you've got an OA invite and 48 hours, this is a pattern worth locking in tonight. The twist here is that shared endpoints count as overlap, so [1,4] and [4,5] collapse into [1,5]. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment.

The problem

Given an array of closed intervals where intervals[i] = [start_i, end_i], merge every pair of overlapping intervals.
Return the non-overlapping intervals that cover every input interval, sorted by start value. Intervals that share an endpoint are considered overlapping.

Function
mergeIntervals(intervals: int[][]) → int[][]

Examples
Example 1
intervals = [[1,3],[2,6],[8,10],[15,18]]
return = [[1,6],[8,10],[15,18]]
Intervals [1,3] and [2,6] overlap, so they merge into [1,6].
Example 2
intervals = [[1,4],[4,5]]
return = [[1,5]]
The intervals share endpoint 4, so they merge.
Example 3
intervals = [[8,10],[1,4],[2,3],[15,18],[6,9],[3,7],[17,20],[12,12]]
return = [[1,10],[12,12],[15,20]]
After sorting, the first five intervals form one overlap chain. The point interval remains separate, and the last two intervals merge.

Constraints
1 <= intervals.length <= 10000
intervals[i].length == 2
0 <= start_i <= end_i <= 10000

Reported by candidates. Source: FastPrep

Pattern and pitfall

Sort the intervals by start value. Then walk through 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 interval to the result and start a new one. Sorting costs O(n log n), the sweep is O(n). The pitfalls are small but they bite. Use <= for the comparison, not <, because touching endpoints merge. Use max for the end, since a later interval can sit entirely inside the current one, like [2,3] inside [1,4]. Don't forget the single-point interval [12,12], which stays separate when nothing touches it. Also don't mutate the input in surprising ways if the signature expects a fresh return. If you blank on the sweep logic during the live OA, StealthCoder is the hedge that reads the problem and hands you the working solution.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Merge 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as merge intervals. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Zscaler's OA.

Zscaler reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Merge Intervals FAQ

How hard is the Zscaler Merge Intervals question really?+

It's a medium on paper but friendly once you know the pattern. Sort, then one pass. Most failures come from edge cases like touching endpoints or nested intervals, not from the core idea. If you can code the sweep from memory, you're fine.

What's the trick to solving it?+

Sort by start first. After that, overlapping intervals are always adjacent, so one pass is enough. Compare the next start to the current end, and extend the end with max when they overlap. Without sorting, you'd be stuck comparing pairs.

Do intervals that share an endpoint really merge?+

Yes. The problem states it directly, and Example 2 shows [1,4] and [4,5] becoming [1,5]. Use start <= currentEnd as your overlap check. Using strict less-than is the most common way to fail the hidden tests.

Does the input size matter for my approach?+

With up to 10000 intervals, an O(n^2) pairwise merge might pass but it's risky and clumsy. Sorting plus a linear sweep runs in O(n log n) and is simple. Values are capped at 10000, so a difference-array approach also works, but sorting is cleaner.

How do I prepare in 48 hours?+

Write the sort and sweep from scratch twice, then test on nested intervals, single-point intervals, and a one-element input. Run Example 3 by hand. That covers nearly every trap. Related interval problems like insert interval use the same skeleton.

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

OA at Zscaler?
Invisible during screen share
Get it