Solve Rooms and Top-K Streams
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving skills across interval scheduling/resource allocation and streaming top-k maintenance, testing competency in data structures for ordered queries, tie-breaking logic, stateful stream processing, and analysis of time and space trade-offs.
Part 1: Minimum Concurrent Meeting Rooms
Constraints
- 0 <= len(intervals) <= 200000
- Each interval has exactly two integers: [start, end]
- -10^9 <= start < end <= 10^9
- Intervals may be given in any order
- A meeting ending at time t does not overlap a meeting starting at time t
Examples
Input: ([[0, 30], [5, 10], [15, 20]],)
Expected Output: 2
Explanation: The meeting [0, 30) overlaps with both shorter meetings, but the shorter meetings do not overlap each other.
Input: ([],)
Expected Output: 0
Explanation: No meetings require no rooms.
Input: ([[1, 2], [2, 3], [3, 4]],)
Expected Output: 1
Explanation: Each meeting starts exactly when the previous one ends, so one room is enough.
Input: ([[1, 5], [2, 6], [3, 7], [4, 8]],)
Expected Output: 4
Explanation: All four meetings overlap during the time range [4, 5).
Input: ([[10, 20], [-5, 0], [0, 10], [5, 15]],)
Expected Output: 2
Explanation: Meetings at exact end/start boundaries can reuse rooms, but [0, 10) overlaps with [5, 15), and [5, 15) overlaps with [10, 20).
Hints
- Separate the start times and end times, then process them in sorted order.
- When the next meeting starts before the earliest current meeting ends, you need a new room.
Part 2: Maintain Top K Items in a Stream
Constraints
- 0 <= k <= 100000
- 0 <= len(stream) <= 200000
- Each stream entry has exactly two integers: [itemId, score]
- -10^9 <= itemId <= 10^9
- -10^9 <= score <= 10^9
- Each itemId appears at most once in stream
Examples
Input: (2, [[1, 50], [2, 60], [3, 55], [4, 60]])
Expected Output: [[[1, 50]], [[2, 60], [1, 50]], [[2, 60], [3, 55]], [[2, 60], [4, 60]]]
Explanation: After the final item, the two highest scores are both 60, ordered by smaller itemId first.
Input: (0, [[1, 10], [2, 20], [3, 30]])
Expected Output: [[], [], []]
Explanation: When k is 0, no items are maintained, but one empty snapshot is returned after each add.
Input: (3, [])
Expected Output: []
Explanation: An empty stream produces no snapshots.
Input: (5, [[10, -1], [5, -1], [7, 0]])
Expected Output: [[[10, -1]], [[5, -1], [10, -1]], [[7, 0], [5, -1], [10, -1]]]
Explanation: Because fewer than k items have been seen, all seen items are returned, sorted by ranking.
Input: (2, [[5, 10], [2, 10], [3, 10]])
Expected Output: [[[5, 10]], [[2, 10], [5, 10]], [[2, 10], [3, 10]]]
Explanation: All scores tie, so the two smallest itemIds are kept.
Hints
- Keep only the best k items seen so far. The item you most want to remove is the worst item among the current top k.
- Use a min-heap where the root is the worst kept item: lowest score first, and for equal scores, larger itemId first.