Consolidate On-Call Rotation Segments
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates proficiency with interval manipulation, set management, sorting and event ordering, and the use of appropriate data structures in the Coding & Algorithms domain.
Constraints
- 0 <= len(intervals) <= 100000
- -10^9 <= start < end <= 10^9
- `name` is a non-empty string
- Intervals may overlap, touch at boundaries, or contain duplicate rows
Examples
Input: [{'name': 'A', 'start': 10, 'end': 50}, {'name': 'B', 'start': 20, 'end': 60}, {'name': 'C', 'start': 30, 'end': 40}, {'name': 'D', 'start': 30, 'end': 40}]
Expected Output: [{'start': 10, 'end': 20, 'names': ['A']}, {'start': 20, 'end': 30, 'names': ['A', 'B']}, {'start': 30, 'end': 40, 'names': ['A', 'B', 'C', 'D']}, {'start': 40, 'end': 50, 'names': ['A', 'B']}, {'start': 50, 'end': 60, 'names': ['B']}]
Explanation: This is the sample scenario. The active set changes only at times 10, 20, 30, 40, 50, and 60.
Input: []
Expected Output: []
Explanation: With no intervals, there is no on-call coverage to report.
Input: [{'name': 'A', 'start': 1, 'end': 3}, {'name': 'A', 'start': 3, 'end': 5}]
Expected Output: [{'start': 1, 'end': 5, 'names': ['A']}]
Explanation: Because intervals are half-open, A is active continuously from 1 to 5. The two adjacent ranges must be merged into one maximal segment.
Input: [{'name': 'A', 'start': 1, 'end': 4}, {'name': 'A', 'start': 1, 'end': 4}, {'name': 'B', 'start': 2, 'end': 3}]
Expected Output: [{'start': 1, 'end': 2, 'names': ['A']}, {'start': 2, 'end': 3, 'names': ['A', 'B']}, {'start': 3, 'end': 4, 'names': ['A']}]
Explanation: The duplicate A interval should not cause A to appear twice. B is only active from 2 to 3.
Input: [{'name': 'X', 'start': -5, 'end': -1}, {'name': 'Y', 'start': -3, 'end': 2}, {'name': 'Z', 'start': 4, 'end': 6}]
Expected Output: [{'start': -5, 'end': -3, 'names': ['X']}, {'start': -3, 'end': -1, 'names': ['X', 'Y']}, {'start': -1, 'end': 2, 'names': ['Y']}, {'start': 4, 'end': 6, 'names': ['Z']}]
Explanation: This checks negative times, overlap, and that the uncovered gap from 2 to 4 is omitted.
Hints
- Think of each interval as creating two events: one that adds a name at `start`, and one that removes a name at `end`.
- The active on-call set can only change at event times. Sweep through times in sorted order, and track counts per name so overlapping intervals for the same person are handled correctly.