As reported: Booking.com SDE 2 (Fintech, Amsterdam), 2026, round 2 (DSA, one hour; line sweep + sliding window). Given an array of intervals
[start_i, end_i]and an integerK(the time window size), return the maximum number of intervals that overlap with any single time window of sizeK. Coding the optimal line sweep directly, explaining the complexity and dry-running it went well.
You are given intervals, where intervals[i] = [start_i, end_i], and an integer K. A time window of size K is any range [t, t + K] for a real number t. An interval overlaps the window when they share at least one point (both are closed: start_i <= t + K and end_i >= t).
Return the maximum number of intervals that overlap one window of size K.
Example 1
intervals = [[1,3],[5,6],[8,10]], K = 2
Output: 2
The window [3,5] touches [1,3] and [5,6]. No window of length 2 reaches all three.
Example 2
intervals = [[1,2],[4,5],[7,8]], K = 1
Output: 1
The gaps between intervals are 2 long, so a window of length 1 touches at most one.
Example 3
intervals = [[1,10],[2,3],[4,5],[6,7],[8,9]], K = 4
Output: 4
The window [2,6] touches [1,10], [2,3], [4,5] and [6,7].
Constraints: 1 <= intervals.length <= 10^5, 1 <= start_i <= end_i <= 10^9, 0 <= K <= 10^9.
Code runner format: one JSON value per line: intervals, K. Implement maxOverlapInWindow(intervals, k) returning an integer.
Clarifications
- Endpoints are inclusive on both the intervals and the window: touching at a single point counts.
K = 0means the window is a single point.