Compute peak busy dashers with overlaps
Company: Rippling
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates skills in interval-overlap reasoning, sweep-line algorithm design, event creation and deduplication, comparator and tie-breaking subtleties, and asymptotic time/space complexity analysis within the Coding & Algorithms domain.
Part 1: Peak Busy Dashers with Overlapping Orders
Constraints
- 0 <= len(logs) <= 200000
- 0 <= dasherId <= 10^9
- 0 <= startTime < endTime <= 10^9
- endTime is exclusive
Examples
Input: [[1, 1, 5], [1, 2, 6], [2, 4, 7]]
Expected Output: 2
Explanation: Dasher 1 is busy continuously from 1 to 6 after merging its overlapping orders. Dasher 2 overlaps from 4 to 6, so the peak is 2.
Input: [[1, 1, 3], [2, 3, 5]]
Expected Output: 1
Explanation: Because endTime is exclusive, the first order ends before the second begins at time 3.
Hints
- First combine each dasher's intervals so overlapping or touching intervals become one busy block.
- After merging per dasher, the problem becomes a standard interval overlap sweep.
Part 2: Sweep-Line Busy Dashers in O(n log n)
Constraints
- 0 <= len(logs) <= 200000
- 0 <= dasherId <= 10^9
- 0 <= startTime < endTime <= 10^9
- You should target O(n log n) time
Examples
Input: [[1, 1, 4], [1, 1, 3], [2, 2, 5]]
Expected Output: 2
Explanation: Dasher 1 starts two orders at time 1 but still counts as only one busy dasher. Dasher 2 overlaps later, so the peak is 2.
Input: [[1, 1, 3], [1, 3, 5], [2, 3, 4]]
Expected Output: 2
Explanation: At time 3, dasher 1's first order ends before its second begins. Dasher 2 also starts at 3, so the peak becomes 2.
Hints
- Sort events by (time, delta, dasherId) if you encode end as -1 and start as +1.
- Multiple starts for the same dasher at the same time should not raise the busy count more than once; track active orders per dasher.
Part 3: Correctly Sort Sweep-Line Events
Constraints
- 0 <= len(events) <= 200000
- 0 <= dasherId <= 10^9
- 0 <= time <= 10^9
- delta is either -1 or 1
Examples
Input: [[2, 5, 1], [1, 3, 1], [1, 5, -1]]
Expected Output: [[1, 3, 1], [1, 5, -1], [2, 5, 1]]
Explanation: Time 3 comes before time 5. At time 5, the end event comes before the start event.
Input: [[2, 4, 1], [1, 4, -1], [3, 4, 1]]
Expected Output: [[1, 4, -1], [2, 4, 1], [3, 4, 1]]
Explanation: All events share the same time, so delta decides first and dasherId breaks the remaining tie.
Hints
- The sweep-line must move through time globally, so time has to be the first sort key.
- When two events share the same time, an end event should come before a start event because endTime is exclusive.
Part 4: Peak Concurrent Orders When Dashers Cannot Overlap Orders
Constraints
- 0 <= len(logs) <= 200000
- 0 <= dasherId <= 10^9
- 0 <= startTime < endTime <= 10^9
- Orders for the same dasher do not overlap
Examples
Input: [[1, 1, 4], [2, 2, 5], [3, 3, 6]]
Expected Output: 3
Explanation: All three orders overlap between times 3 and 4.
Input: [[1, 1, 3], [2, 3, 5], [3, 5, 7]]
Expected Output: 1
Explanation: Because endTime is exclusive, these intervals only touch and never overlap.
Hints
- You no longer need per-dasher active counts, because each order can be counted directly.
- Use the standard interval sweep: add +1 at starts and -1 at ends, with ends processed before starts at the same time.