Compute peak concurrent drivers in 24 hours
Company: Rippling
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates the ability to design algorithms and data structures for streaming interval data, specifically computing peak distinct-driver concurrency over a half-open 24-hour time window with attention to boundary inclusivity and distinct-count semantics.
Constraints
- 1 <= len(operations) <= 20000
- For every add operation, start_time < end_time
- -10^9 <= start_time, end_time, T <= 10^9
- driver_id is an integer
- The query window is always the half-open interval [T - 24, T)
Examples
Input: ([("add", 1, 0, 10), ("add", 2, 5, 15), ("query", 12)],)
Expected Output: [2]
Explanation: In the window [-12, 12), driver 1 is active on [0, 10) and driver 2 on [5, 12). The peak overlap is 2 on [5, 10).
Input: ([("add", 1, 0, 10), ("add", 1, 5, 12), ("add", 2, 6, 8), ("query", 9), ("query", 13)],)
Expected Output: [2, 2]
Explanation: Driver 1's two intervals overlap, so they count as one distinct driver. Driver 2 overlaps driver 1 on [6, 8), making the peak 2 for both queries.
Input: ([("add", 1, 0, 5), ("add", 2, 5, 10), ("query", 10)],)
Expected Output: [1]
Explanation: End times are exclusive. Driver 1 is inactive at time 5, exactly when driver 2 becomes active, so the peak is never 2.
Input: ([("add", 1, 0, 30), ("query", 15), ("add", 2, 10, 20), ("query", 15)],)
Expected Output: [1, 2]
Explanation: Queries are processed in stream order. The first query sees only driver 1. The second query uses the same T but also sees driver 2's later insertion, so the peak becomes 2.
Input: ([("query", 0), ("add", 1, -30, -10), ("add", 2, -5, 5), ("query", 0)],)
Expected Output: [0, 1]
Explanation: The first query has no inserted intervals yet, so the answer is 0. For the second query, the window is [-24, 0): driver 1 is active on [-24, -10) and driver 2 on [-5, 0), so the peak is 1.
Hints
- A single driver must never be counted twice at the same time. Maintain each driver's coverage as a sorted list of merged intervals, and when a new interval arrives, only add the parts not already covered by that driver.
- After coordinate compression, every newly uncovered sub-interval becomes a range +1 update on a segment tree, and every query becomes a range maximum query on [T - 24, T).