What they’re really asking
If you only remember one thing, say this in the first minute: “This is merge-intervals. Every GPU is idle exactly when no GPU is busy, so I take the union of all busy intervals and report the gaps inside a window. I’ll use half-open intervals, so a job ending at 10 and one starting at 10 leave no gap. Sorting costs O(N log N), then one pass. I’ll test it against a brute force that checks every minute.”
The rest of this page explains each part of that answer.
A GPU (graphics processing unit) is the expensive chip AI models run on. For this problem you can forget that. A GPU here is just a machine that is busy or idle at each moment, and each GPU comes with a list of busy periods. The question asks when all of them are idle at once.
What makes it more than a warm-up is not the first part. It is the follow-ups. Expect the interviewer to push on three things: what happens where two intervals touch, what changes when there are many already-sorted lists, and what changes when the question becomes “at least k GPUs” or “a job that needs k GPUs”. The last of those looks like a small change and needs a different algorithm.
(A DesignGurus prep page on Fireworks’ process says “Reported problems include finding the global idle intervals across many GPUs” and that candidates describe phone screens as “live coding at medium difficulty” DesignGurus. It is one secondary report with no date on the question, so the real follow-ups are unknown.)
Words you need first
- Interval: a busy period with a start and an end, written
(start, end). - Half-open interval: written
[start, end). The start minute is included and the end minute is not. So[0, 2)and[2, 4)touch but don’t overlap, and together they make one busy stretch[0, 4). - Union: all the busy intervals glued together wherever they overlap or touch.
- Window: the range
[lo, hi)you care about. Before the first job and after the last, every GPU is idle forever, so the answer needs a bound.
Clarify first
Ask these before you write code. Each has a default you can propose.
- Convention: “Half-open
[start, end)? Then a GPU busy until 10 and another from 10 leave no gap.” - Window: “Idle within
[lo, hi)? Outside all jobs, GPUs are idle forever.” - Input: one list per GPU, each sorted? Can one GPU’s own intervals overlap (two jobs on one GPU)?
- Output: sorted
(start, end)pairs that don’t touch each other. - Size: how many intervals in total (N) and how many GPUs (G)? That decides between a full sort and a k-way merge, explained below.
Answer
The example to follow
Three GPUs, window [0, 10). GPU 0 is busy [0, 2) and [6, 8). GPU 1 is busy [2, 4) and [7, 9). GPU 2 is
busy [5, 7). As a timeline, one column per minute, # busy and . idle:
minute 0 1 2 3 4 5 6 7 8 9 10
GPU 0 ########................########........
GPU 1 ........########............########....
GPU 2 ....................########............
all idle ^^^^ ^^^^
Every GPU is idle from 4 to 5 and from 9 to 10. That is what the code must return.
The core solution
Sort all busy intervals by start. Walk them with a cursor, the end of the busy union so far. When an interval starts after the cursor, the space between them is idle.
def all_idle(busy_by_gpu, lo, hi):
spans = sorted(iv for gpu in busy_by_gpu for iv in gpu)
idle, cursor = [], lo
for s, e in spans:
if s > cursor:
idle.append((cursor, min(s, hi)))
cursor = max(cursor, e)
if cursor >= hi:
break
if cursor < hi:
idle.append((cursor, hi))
return [(s, e) for s, e in idle if s < e]
The comparison is s > cursor, not >=. That single character is the half-open rule: a start equal to the
cursor touches the union and leaves no gap.
Trace the example. Sorted, the spans are (0,2) (2,4) (5,7) (6,8) (7,9):
| Span | Cursor before | What happens | Cursor after | What to say in the interview |
|---|---|---|---|---|
| (0, 2) | 0 | no gap | 2 | ”Start equals the cursor, so nothing is idle.” |
| (2, 4) | 2 | touches, no gap | 4 | ”Half-open: touching intervals join.” |
| (5, 7) | 4 | gap (4, 5) | 7 | ”Start is past the cursor; that space is idle.” |
| (6, 8) | 7 | overlaps | 8 | ”Overlap only moves the cursor.” |
| (7, 9) | 8 | overlaps | 9 | |
| end | 9 | gap (9, 10) | — | “Close the window at hi.” |
The function returns [(4, 5), (9, 10)] (run on 2026-10-12). The cost is O(N log N) for the sort, then one
pass.
When each GPU’s list is already sorted
Re-sorting everything wastes the order you were given. A k-way merge combines several sorted lists into
one sorted stream by keeping the next item of each list in a heap (a priority queue that always returns the
smallest item). Replace sorted(...) with heapq.merge(*busy_by_gpu): O(N log G) time, O(G) memory,
and it streams, so the lists don’t have to fit in memory.
Say it like this: “With G sorted lists I don’t need to re-sort. A heap merge costs N log G and holds G items, not N.”
The follow-ups
The family walkthrough works through two more stages with code. Each was tested against a brute force that checks every integer time (3,000 random cases passed when run on 2026-10-12).
- At least k GPUs idle. A union can’t count, so use a sweep line: turn each interval into a +1 event at
its start and a −1 event at its end, sort the events (ends before starts at a tie, which is the half-open
rule), and walk them with a running busy count. Report where
G − busy ≥ k. In the example, with k = 2, the answer is[(0, 6), (8, 10)]. - Earliest slot for a job on k GPUs for d minutes. “k idle at every moment” is not enough, because the job
needs the same k GPUs throughout. With GPU 0 busy on
[0, 2), GPU 1 on[2, 4)and GPU 2 free, two GPUs are idle at every moment of[0, 4), but no pair is idle for all of it. Try candidate starts (the window start and every busy end) and count GPUs free for[t, t + d)by binary search.
Our example shows the trap. A job that needs 2 GPUs for 3 minutes looks like it can start at minute 0, because
the k = 2 answer covers [0, 6). But at minute 0 only GPU 2 is free for three minutes. The real answer is
minute 2, on GPUs 0 and 2.
Say it like this: “‘Two idle at every moment’ isn’t ‘the same two idle throughout’. Here’s a three-GPU counterexample, so I’ll search candidate start times instead.”
Say the half-open rule once and use > versus >= consistently. Most bugs in this problem are boundary bugs.
The arithmetic behind the numbers (for a Staff-level follow-up)
- Why only candidate starts: if a start t works for some k GPUs, sliding it earlier keeps it working until it hits the window start or the end of some busy interval on one of those GPUs. So the earliest start is one of those times: at most N + 1 candidates.
- Cost of the candidate search: C candidates × G GPUs × a binary search of log n (n = longest per-GPU list), at most O(N × G log n). If that is too slow, sweep the candidates in order and keep each GPU’s next busy start up to date instead of searching again.
- Sweep cost: 2N events, sorted in O(N log N), then one pass.
Senior vs Staff
GPU idle intervals
Senior
- Sort and merge with a consistent convention, brute-force test, k-way merge when lists are sorted.
Staff
- Raises the same-k-GPUs counterexample before coding the scheduling follow-up, and says when the candidate-start search gets too slow and what replaces it.
Small-team version
Low relevance as code. The question it answers, “when are my GPUs idle?”, does matter to a team running its own model on a few GPUs. But your monitoring stack’s utilization graphs answer it without new code.
What to do instead:
- Watch: the utilization graphs you already have.
- Buy, don’t build: if you schedule multi-GPU batch jobs often enough that idle gaps cost money, look at a scheduler with gang scheduling (it starts a job only when all the GPUs it needs are free at once) rather than writing one.
What “cost money” means, with assumed numbers: 4 GPUs at $2 per GPU-hour that sit idle 30% of a month waste about $1,750 a month (4 × 730 hours × 0.3 × $2 ≈ $1,750).
Pitfalls
- Mixing closed and half-open comparisons. Touching intervals then join in one place and leave a gap in another.
- Forgetting the window. Leading and trailing idle time goes missing or becomes infinite.
- “At least k idle at every moment” for a job. The job needs the same GPUs for its whole duration.
- Merging per GPU only. Intervals overlap across GPUs too, and the union across GPUs is the point.
Go deeper
- Family walkthrough: Short DSA screens with an infrastructure flavor, where this problem is worked in four stages with code and tests.
- The other screens in the family: padding, scheduling.