InterviewLegend

Intervals

Alex Chen20 min readUpdated 181 real interview reports
In this lesson
  • See why interval problems are the signature coding family at Netflix and how the round frames them
  • Drill two reusable skeletons: the sort-by-start sweep and the +1/-1 sweep line that counts concurrency
  • Solve the three problems Netflix reuses most and recognize them as three framings of one pattern

Netflix keeps its coding round lean, so the few patterns that recur carry real weight. Intervals are the signature one. Merge Intervals shows up more than any other problem in the whole loop. The two that travel with it, the minimum-rooms scheduling question and the flowers-in-bloom counting question, are the same idea underneath. Learn this family and you've covered a real slice of what Netflix asks.

Why intervals, and not something else? The domain. Netflix runs on overlapping spans of time. A title is licensed in a region for a window. A live event books encoder and ingest capacity for its broadcast. Playback sessions hold a slot on a server. Ad impressions stack inside a frequency-capping window. Each one is a start, an end, and a question about overlap or how many things run at once. Interval problems are that question stripped to a list of pairs.

How Netflix frames it

Netflix interviewers wrap the problem in a product story instead of handing you the textbook version. Merge the windows a title is available in. Size the transcoding workers a release needs when jobs overlap. Count how many streams are live at a given second. Same pairs underneath, every time. They're watching whether you can see the interval skeleton through the streaming vocabulary and say so.

One medium problem, a clock, and a real conversation. Often there's no run button, so you verify by walking an example by hand. And expect a follow-up. It flips "merge these" into "now how many overlap at the busiest moment," which is the version that maps to capacity planning. Solve what they asked cleanly, call your sort and your complexity as you go, leave room to extend.

The Netflix angle
Senior loops reward the candidate who ties the toy problem back to a real system. Finish merging availability windows, then say where this lives: it dedupes the spans a title is watchable in, or collapses overlapping retry windows in a pipeline. Count concurrent meetings, then name it as sizing a worker pool or counting live sessions on a box. The mechanics are the easy half. Whether you think in systems while you write them is the half that moves you up a level, because this round keeps sliding into a design conversation.

Almost every interval problem collapses to one move: sort the endpoints, then sweep left to right carrying a single running fact. Which fact depends on the question. Merging? Carry the end of the open interval and stretch it when the next start falls inside. Counting overlap? Carry a live counter, up on a start, down on an end. Pick the running quantity from the prompt and the sweep writes itself.

The two templates to know

Two skeletons cover the whole family. One merges overlapping spans. The other counts how many are active at once. Drill each until it's reflex, so in the room your head is on the Netflix framing while the sweep runs underneath on autopilot.

Pattern 1: Sort by start, then sweep and merge

Reach for this when the answer is a smaller set of combined spans, like collapsing the windows a title is available in. Sort the intervals by their start. Now anything that overlaps the one you are holding has to come next, because a later start cannot reach back past an earlier one. So you keep the most recent merged interval and, for each new one, either stretch its end or start a fresh interval.

python
def merge(intervals):
intervals.sort(key=lambda iv: iv[0]) # by start: overlaps now sit adjacent
merged = []
for start, end in intervals:
# overlaps the open interval if it starts at or before that interval's end
if merged and start <= merged[-1][1]:
merged[-1][1] = max(merged[-1][1], end) # stretch the open interval
else:
merged.append([start, end]) # a gap: begin a new open interval
return merged

The detail that decides correctness is the overlap test, start <= merged[-1][1]. Sorting by start guarantees the new interval begins no earlier than the open one, so they touch exactly when the new start has not passed the open end. When they do touch, the merged end is the larger of the two ends. It is never just the new one, because the open interval might already reach further right than this one does. Forget that max and a short interval nested inside a long one wrongly shrinks the result.

Pattern 2: Sweep the endpoints with +1 and -1

Reach for this when the question is "how many are active at once," like the peak number of concurrent streams or the workers a release needs. Pull every start and every end out as separate events, sort each list, then walk time forward. A start lifts the live count by one and an end drops it by one. The largest the count ever reaches is the peak overlap.

python
def max_concurrent(intervals):
starts = sorted(s for s, e in intervals) # all begin times, ascending
ends = sorted(e for s, e in intervals) # all finish times, ascending
live = 0 # how many are active right now
peak = 0 # the most we have ever seen active at once
j = 0 # how far we have advanced through the finishes
for start in starts:
while j < len(ends) and ends[j] <= start:
live -= 1 # something finished at or before this start: free it first
j += 1
live += 1 # this one begins
peak = max(peak, live)
return peak

Two endpoints can land on the same instant, so the tie rule matters. Here an end at the same moment as a start is processed first, with ends[j] <= start, which treats a span that finishes exactly as another begins as not overlapping. That is right when an end means the room or the worker frees up at that instant. If the problem instead counts a shared endpoint as an overlap, you switch the test to < so the start is counted before the end. Decide which the prompt means and say it.

The two templates share a backbone: sort, then sweep carrying one number. Merging carries the open interval's end. Concurrency counting carries a running tally that ticks up and down. Once you can name which running quantity the question wants, the rest is the same left-to-right pass. That is why these problems feel interchangeable once you have drilled them, and why a Netflix interviewer can swap availability windows for encoder jobs and still be asking the same question.

For the underlying mechanics at a slower pace, Foundations covers sorting and the intervals pattern in depth. Here the focus stays on the Netflix framing and the follow-up each problem invites.

Asked at Netflix

Three problems, ordered the way the round builds. Merge Intervals is the core and the one to nail first. The minimum-rooms question is the concurrency sweep, framed as capacity. The flowers question is the same sweep again with the count read off precomputed prefix sums. Solve each in the editor before revealing the walkthrough, and narrate your sort and your complexity the way you would in the room. Watch how each one restates the last in new terms while the left-to-right pass underneath holds steady.

The Netflix angle
Drill these three back to back, because the whole lesson is recognizing the family. Merge Intervals teaches the sort-and-coalesce move. Minimum rooms reuses the sorted endpoints but counts instead of merging. The flowers question keeps that same sorted-endpoints idea and answers many time queries at once. A Netflix interviewer who opens with one and pivots to another is checking whether you notice you already hold the tool. Treat the second as a small edit on the first, not a fresh problem.
1

Title Availability Windows: Merge Intervals

MediumSignature problem
Problem
You're given an array intervals where intervals[i] = [start, end] represents a span of time. Merge every set of overlapping intervals and return an array of the non-overlapping intervals that cover all the input spans. Two intervals overlap when one starts at or before the other ends, so [1,4] and [4,5] merge into [1,5].

Read it as the availability windows of a title: overlapping or touching windows should collapse into one continuous span.

Input: intervals = [[1,3],[2,6],[8,10],[15,18]]

Output: [[1,6],[8,10],[15,18]]

[1,3] and [2,6] overlap, so they merge into [1,6]. The other two touch nothing and stay as they are.

Input: intervals = [[1,4],[4,5]]

Output: [[1,5]]

They share the endpoint 4, which counts as touching, so they merge into [1,5].

How this uses the pattern

This is Pattern 1. Sort by start so overlapping intervals sit next to each other, then sweep once, stretching the open interval whenever the next start falls at or before its end.

Title Availability Windows: Merge Intervals

Loading...
Click "Run" to execute your code

Test Cases

Input: [[[1,3],[2,6],[8,10],[15,18]]]Expected: [[1, 6], [8, 10], [15, 18]]
Input: [[[1,4],[4,5]]]Expected: [[1, 5]]
Input: [[[1,4],[2,3]]]Expected: [[1, 4]]
Input: [[[1,4],[5,6]]]Expected: [[1, 4], [5, 6]]
The Netflix angle
Merge Intervals is the most consistently reported coding problem in the Netflix loop, so treat it as the one you cannot fumble. Netflix tends to dress it as a real overlap-collapsing task, like deduping the regional windows a title is watchable in or coalescing overlapping retry spans in a pipeline. Sort by start, name why that makes overlaps adjacent, and use max on the end so a long span swallowing a short one is handled. Just land a clean, confident sweep on the first thing they ask.
Take it further
The common pivot is to stop merging and start counting: what is the largest number of intervals that overlap at any single moment. That is the capacity question, sizing a worker pool or counting peak concurrent streams, and it is the next problem in this lesson. You answer it by splitting the intervals into start and end events, sorting both, and sweeping a running counter up on each start and down on each end. Worked through in the walkthrough.
2

Peak Concurrent Streams: Meeting Rooms II

MediumFrequently asked
Problem
You're given an array intervals where intervals[i] = [start, end] is a meeting's time span. Return the minimum number of rooms needed so that no two meetings ever share a room. A room is reusable: a meeting that ends at time t frees its room for a meeting that starts at t.

The answer equals the largest number of meetings that are ever running at the same instant. Read it as sizing a pool: how many transcoding workers does a wave of overlapping jobs need, or how many streams are live at peak.

Input: intervals = [[0,30],[5,10],[15,20]]

Output: 2

[0,30] runs the whole time. [5,10] overlaps it, needing a second room. [15,20] reuses the room [5,10] freed. Peak overlap is 2.

Input: intervals = [[7,10],[2,4]]

Output: 1

The two never overlap, so one room handles both in turn.

How this uses the pattern

This is Pattern 2, the concurrency sweep. Pull out all the starts and all the ends, sort each, and walk forward: a start needs a room, an end frees one. The peak number of rooms in use at once is the answer.

Peak Concurrent Streams: Meeting Rooms II

Loading...
Click "Run" to execute your code

Test Cases

Input: [[[0,30],[5,10],[15,20]]]Expected: 2
Input: [[[7,10],[2,4]]]Expected: 1
Input: [[[1,5],[5,8],[8,9]]]Expected: 1
Input: [[[1,5],[2,6],[3,7]]]Expected: 3
The Netflix angle
This is Merge Intervals reframed as capacity, and it tends to follow the merge question in a Netflix loop precisely because it reuses the sorted endpoints. The framing is usually a real pool-sizing problem: the minimum encoder workers for a batch of overlapping jobs, or the peak concurrent sessions a server must hold. The thing to make explicit is that an end at the same instant as a start frees the room first, so a meeting ending at t and one starting at t share a room. Say that tie rule out loud before you code.
Take it further
Netflix often asks for the heap variant as the other natural way to see it, since it mirrors how a real scheduler hands out a fixed resource. Sort meetings by start and keep a min-heap of the end times of meetings currently using a room. For each meeting, if the earliest end on the heap is at or before this start, that room is free, so pop it. Push this meeting's end. The heap's size at the end, or its largest size during the run, is the number of rooms. Worked through below.
3

Streams Live at Playback Time: Number of Flowers in Full Bloom

HardCommonly asked
Problem
You're given flowers, where flowers[i] = [start, end] means flower i is in bloom from time start to time end, inclusive on both ends. You're also given an array people, where people[j] is the time person j arrives. For each person, return how many flowers are in full bloom when they arrive.

A flower counts for a person if start <= arrival <= end. Read it as: at the instant each viewer presses play, how many spans are currently active.

Input: flowers = [[1,6],[3,7],[9,12],[4,13]], people = [2,3,7,11]

Output: [1,2,2,2]

At time 2 only [1,6] is open. At 3, [1,6] and [3,7]. At 7, [3,7] and [4,13]. At 11, [9,12] and [4,13].

Input: flowers = [[1,10],[3,3]], people = [3,3,4]

Output: [2,2,1]

At time 3 both flowers are open; the single-instant flower [3,3] counts. At 4 only [1,10] remains.

How this uses the pattern

Same concurrency idea as the rooms problem, but answered for many query times at once. Sort the starts and the ends separately. The count active at time t is (starts at or before t) minus (ends strictly before t), each found by binary search.

Streams Live at Playback Time: Number of Flowers in Full Bloom

Loading...
Click "Run" to execute your code

Test Cases

Input: [[1,6],[3,7],[9,12],[4,13]], [2,3,7,11]Expected: [1, 2, 2, 2]
Input: [[1,10],[3,3]], [3,3,4]Expected: [2, 2, 1]
Input: [[1,1]], [1,2]Expected: [1, 0]
Input: [[1,10]], [5]Expected: [1]
The Netflix angle
This is the concurrency sweep again, and Netflix uses it to see whether you notice the meeting-rooms count returning under a new prompt, often right after asking that one. The new wrinkle is that you answer many query times, not one peak, so a fresh per-person sweep would be too slow. It comes apart with a small decomposition. A flower is open at time t when it has already started and has not yet ended. So the count is the number of starts at or before t minus the number of ends before t. Sort the starts and ends once, then binary-search each person's time. State that decomposition and the rest is two lookups.
Take it further
Watch the inclusive-end boundary, because it is where a wrong answer hides. A flower with end = t is still blooming at time t, so you count it. That means starts use bisect_right (everything that started at or before t) while ends use bisect_left (only those that ended strictly before t, since one ending exactly at t is still open). Flip either side and the counts drift by one at every shared boundary. The walkthrough shows exactly which bisect goes where and why.

Key Takeaways

  • ✓Intervals are the signature Netflix coding family because the domain is overlapping time spans: availability windows, encoder jobs, live sessions, ad-capping windows. Merge Intervals is the most consistently reported problem in the loop.
  • ✓Two skeletons cover the family. Pattern 1 sorts by start and sweeps, stretching one open interval to merge. Pattern 2 splits the endpoints, sorts each, and sweeps a +1/-1 counter to find peak concurrency.
  • ✓The three practice problems are one pattern in three framings. Merge collapses spans, Meeting Rooms II counts the peak overlap as capacity, and Number of Flowers in Full Bloom answers that same count for many query times with binary search. Recognizing the same sweep behind each new prompt is the skill the round tests.
  • ✓Call out the tie rule and the sort, and connect the toy problem to a real Netflix system. Treat the follow-up as a small edit on code you already wrote.
Had your Netflix interview?Tell us what they asked. Reviewed before it goes in, never with your name.
Netflix interview guide

Know what Netflix asks before you walk in.

22 coding · 7 system design · 19 behavioral questions

Get Premium$49 a year · Cancel anytime
Or start freeNo card.

Get their questions first.

InterviewLegend is independent and not affiliated with or endorsed by any company it covers.