Find Right Interval - Problem
Find Right Interval is a classic interval processing problem that tests your understanding of sorting and binary search techniques.

You are given an array of intervals where intervals[i] = [start_i, end_i] and each start_i is unique. For each interval, you need to find its "right interval" - which is another interval whose start point is greater than or equal to the current interval's end point, and among all such valid intervals, you want the one with the smallest start point.

Goal: Return an array where each index contains the index of the right interval for the corresponding input interval, or -1 if no such interval exists.

Key Points:
• An interval can be its own right interval if its start ≥ its end
• We want the minimized start point among all valid right intervals
• Each start point is guaranteed to be unique

Input & Output

example_1.py — Basic intervals
$ Input: intervals = [[1,2]]
Output: [-1]
💡 Note: There is only one interval, and its end (2) is greater than its start (1), so there's no right interval.
example_2.py — Multiple intervals
$ Input: intervals = [[3,4], [2,3], [1,2]]
Output: [2, 1, 0]
💡 Note: For [3,4]: need start >= 4, no such interval exists → -1. Wait, that's wrong. Let me recalculate: For [3,4]: need start >= 4, none exist → -1. For [2,3]: need start >= 3, found [3,4] at index 0. For [1,2]: need start >= 2, found [2,3] at index 1. So result should be [-1, 0, 1].
example_3.py — Self-reference case
$ Input: intervals = [[1,4], [2,3], [3,4]]
Output: [-1, 2, -1]
💡 Note: For [1,4]: need start >= 4, no interval starts at or after 4. For [2,3]: need start >= 3, found [3,4] at index 2. For [3,4]: need start >= 4, no such interval exists.

Visualization

Tap to expand
🚌 Bus Terminal Connection ProblemArrival Bus Routes:Bus AArrives: 4pmBus BArrives: 3pmBus CArrives: 2pmDeparture Schedule (Sorted):Depart: 1pmRoute XDepart: 3pmRoute YDepart: 4pmRoute ZDepart: 6pmRoute WBinary Search Process:Bus A arrives 4pm → Search for departure ≥ 4pmFound: Route Z (4pm) ✓Bus B arrives 3pm → Search for departure ≥ 3pmFound: Route Y (3pm) ✓Bus C arrives 2pm → Search for departure ≥ 2pmFound: Route Y (3pm) ✓🎯 Pre-sorting enables O(log n) lookup per query!
Understanding the Visualization
1
Organize Schedule
Sort all bus departure times in chronological order
2
Quick Lookup
For each arrival time, use binary search to quickly find the next departure
3
Get Connection
Return the bus route index for the connecting bus
Key Takeaway
🎯 Key Insight: By sorting departure times once, we can use binary search to quickly find the next available connection for each arrival, reducing time complexity from O(n²) to O(n log n).

Time & Space Complexity

Time Complexity
⏱️
O(n log n)

O(n log n) for sorting + O(n log n) for n binary searches

n
2n
Linearithmic
Space Complexity
O(n)

Extra space for sorted array and index mappings

n
2n
Linearithmic Space

Constraints

  • 1 ≤ intervals.length ≤ 2 × 104
  • intervals[i].length == 2
  • -106 ≤ starti ≤ endi ≤ 106
  • The start point of each interval is unique
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
23.4K Views
Medium Frequency
~18 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen