Solve meeting-room scheduling and shortest paths
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates proficiency in interval scheduling and resource allocation as well as shortest-path computation in weighted directed graphs, emphasizing event ordering, efficient priority structures and graph traversal techniques.
Meeting Rooms: Attend-All, Minimum Rooms, and Most-Used Room
Constraints
- 1 <= intervals.length <= 2*10^5
- 0 <= start < end <= 10^9
- 1 <= n <= intervals.length
- Intervals are half-open [start, end): touching endpoints do not overlap
Examples
Input: ([[0, 30], [5, 10], [15, 20]], 2)
Expected Output: [false, 2, 1]
Explanation: Meetings [0,30] and [5,10] overlap so attend-all is false. Peak concurrency is 2 (at t=5, [0,30] and [5,10]). With 2 rooms: [0,30]->room0, [5,10]->room1, [15,20]-> room1 is free at 10 so ->room1; room1 hosts 2 meetings.
Input: ([[7, 10], [2, 4]], 2)
Expected Output: [true, 1, 0]
Explanation: Sorted [2,4],[7,10] do not overlap, so attend-all is true and only 1 room is ever needed; room0 hosts both.
Input: ([[1, 5], [8, 9], [8, 9]], 2)
Expected Output: [false, 2, 0]
Explanation: The two identical [8,9] meetings overlap each other, so attend-all is false and 2 rooms are needed at t=8. room0 hosts [1,5] then [8,9] (2 meetings) vs room1's single [8,9]; tie-break/most -> room0.
Input: ([[1, 5], [2, 6], [3, 7]], 3)
Expected Output: [false, 3, 0]
Explanation: All three overlap around t=3, so 3 rooms are needed and attend-all is false. Each room hosts exactly one meeting; smallest index wins -> 0.
Input: ([], 1)
Expected Output: [true, 0, 0]
Explanation: Empty schedule: nobody needs a room.
Input: ([[5, 10]], 1)
Expected Output: [true, 1, 0]
Explanation: A single meeting: attendable, 1 room, hosted by room0.
Input: ([[0, 10], [1, 5], [2, 7], [3, 4]], 2)
Expected Output: [false, 4, 0]
Explanation: At t=3 all four meetings are live, so 4 rooms minimum and attend-all false. With only n=2 rooms, delays push counts to [2,2]; tie -> room0.
Hints
- Sort the meetings by start time first; it serves all three parts.
- For minimum rooms, sweep meetings in start order and keep a min-heap of end times; pop every meeting that has already ended before the current start. The peak heap size is the answer.
- For most-used room, keep one heap of free room indices (to pick the lowest index) and one heap of (free_time, room_index) for busy rooms. When nothing is free, pop the earliest-freeing busy room and re-insert it pushed forward by the meeting's duration.
Shortest Path in a Weighted Directed Graph
Constraints
- 1 <= V <= 2*10^5
- 0 <= edges.length <= 2*10^5
- 1 <= w <= 10^9 (positive weights only)
- Directed edges: [u, v, w] does not imply [v, u, w]
- Return -1 when t is unreachable; distance to self is 0
Examples
Input: (5, [[0, 1, 4], [0, 2, 1], [2, 1, 2], [1, 3, 1], [2, 3, 5]], 0, 3)
Expected Output: 4
Explanation: 0->2 (1) ->1 (2) ->3 (1) = 4, which beats 0->1->3 = 5 and 0->2->3 = 6.
Input: (2, [[0, 1, 7]], 0, 1)
Expected Output: 7
Explanation: Single edge of weight 7.
Input: (3, [[0, 1, 5]], 0, 2)
Expected Output: -1
Explanation: Node 2 has no incoming edges, so it is unreachable.
Input: (1, [], 0, 0)
Expected Output: 0
Explanation: Source equals target with no edges; distance to self is 0.
Input: (4, [[0, 1, 1], [1, 2, 1], [2, 0, 1], [2, 3, 10]], 0, 3)
Expected Output: 12
Explanation: 0->1->2->3 = 1+1+10 = 12; the cycle 0->1->2->0 never helps.
Input: (3, [[0, 1, 2], [1, 0, 2]], 0, 2)
Expected Output: -1
Explanation: Only nodes 0 and 1 are connected; node 2 is isolated and unreachable.
Input: (6, [[0, 1, 7], [0, 2, 9], [0, 5, 14], [1, 2, 10], [1, 3, 15], [2, 3, 11], [2, 5, 2], [3, 4, 6], [5, 4, 9]], 0, 4)
Expected Output: 20
Explanation: 0->2 (9) ->5 (2) ->4 (9) = 20, which beats 0->2->3->4 = 26 and 0->5->4 = 23.
Hints
- All weights are positive, so Dijkstra's algorithm applies — no need for Bellman-Ford.
- Use a min-heap of (distance, node). Skip a popped entry if its distance is greater than the best known distance for that node (lazy deletion).
- You can stop early and return as soon as you pop the target t, since Dijkstra finalizes nodes in nondecreasing distance order.