← Questions
Consolidate on-call rotation segments
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's HackerRank assessment, last asked May 2026. It tests intervals and sorting.
An on-call schedule is a list of segments, each naming a person and a start and end time. Segments of the same person that overlap or touch should become one segment. Return the consolidated schedule in time order.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
consolidate(segments: list[tuple[str, int, int]]) -> list[tuple[str, int, int]]— each segment is(person, start, end)covering[start, end). Merge a person's segments when one starts at or before another ends. Return the result sorted by start time, ties by person.
Example
consolidate([("ana", 0, 8), ("bo", 8, 16), ("ana", 8, 12), ("ana", 20, 24), ("bo", 14, 18)])
-> [("ana", 0, 12), ("bo", 8, 18), ("ana", 20, 24)]
# ana's [0,8) and [8,12) touch; bo's [8,16) and [14,18) overlap
Constraints
- Up to 10^5 segments, unsorted. Times are integers with
start < end. - Segments of different people may overlap (two people on call at once); they are never merged with each other.
Clarifying questions
- Do touching segments (
end == start) merge, or only overlapping ones? - Should different people's segments ever affect each other, for example a handoff that cuts one person's segment short?
- Are the input segments sorted?
- What should happen to a zero-length segment?
What a strong answer covers
- Group by person, sort each group by start, and merge in one pass, extending the current segment while the next start is
<= current end.O(n log n). - Sorting the merged output by start, then person, for a stable result.
- The alternative: sort everything by
(person, start)once. - Tests: touching segments, nested segments, a single segment, two people overlapping, unsorted input.
Follow-ups
- Produce a "who is on call when" timeline: split time into pieces and list everyone on call in each piece.
- Find gaps where nobody is on call.
- Each person has a maximum shift length; report violations after consolidation.
Where it's asked
- Google · HackerRank assessment · Last asked May 202690 min · Two problems on HackerRank · Some early-career assessments enable the built-in assistant; others forbid AI. The invitation says which.
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
