Find integer in most intervals
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: This question evaluates a candidate's ability to reason about interval overlap counting, manage range endpoints efficiently, and balance time-space trade-offs when identifying points that maximize coverage.
Constraints
- 1 <= N <= 200000
- 1 <= ai <= bi <= 10^9
- Intervals are closed: x is covered by [a,b] if a <= x <= b
- If multiple integers tie for maximum coverage, return the smallest integer
Hints
- Transform intervals into events: +1 at ai and -1 at bi+1 (difference array idea for inclusive intervals).
- Sort event positions and compute a running sum (prefix) to track active intervals; the first position reaching a new maximum is your answer.
- If all endpoints are less than a small M, use an array of length M+2 for O(N+M) time via difference array and prefix sums.