This one took me a minute to even parse fully.
Start by clarifying the problem constraints and edge cases, then propose a sweep-line algorithm that processes events in sorted order to compute per-minute concurrent counts. Emphasize deduplication, half-open interval handling, and the O(n log n + M) complexity. Finally, discuss percentile tie-breaking and extension to per-country metrics.
Pro tip: Mention that you would validate the algorithm with a small brute-force implementation and discuss how to handle memory constraints by external sorting or streaming with a bounded priority queue for deduplication.
Confirm interval semantics (half-open), deduplication criteria, percentile definition (e.g., nearest-rank or linear interpolation), and tie-breaking rules. Discuss memory constraints and whether the stream can be sorted externally.
Deduplicate intervals by (start, end) using a hash set or external sort. For each unique interval, generate two events: +1 at start, -1 at end. Store events in a list.
Sort events by timestamp (O(n log n)). Sweep through events, maintaining a running count. For each minute boundary (0 to 1439), record the count after processing all events up to that minute. This yields an array of 1440 counts.
Sort the 1440 counts (or use selection algorithm). For percentile, use nearest-rank method: index = ceil(0.95 * 1440) - 1. For ties, define a consistent rule (e.g., take the higher value or interpolate).
Partition events by country code. For each country, run the same sweep-line algorithm independently, producing per-country per-minute counts. Then compute 95th percentile per country. Discuss memory trade-offs and possible optimizations (e.g., processing countries in batches).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.