Find Each Interval's Nearest Eligible Starting Interval

Quick Overview

Practice finding the original index with the smallest start at or beyond each interval's end. Include negative coordinates, exact equality, and point intervals that select themselves.

Find Each Interval's Nearest Eligible Starting Interval

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Find Each Interval's Nearest Eligible Starting Interval You are given intervals `[start_i,end_i]` with unique starting points. For each interval `i`, find the original index `j` whose start is the smallest start satisfying `start_j >= end_i`. Return these indices in an array aligned with the original interval order. Use `-1` when no eligible start exists. An interval may select itself when its endpoints allow it. ### Function `vector<int> findRightInterval(vector<vector<int>>& intervals)` ### Example 1 ```text Input: intervals = [[4,4],[1,4],[6,8],[8,9]] Output: [0,0,3,-1] ``` The point interval at index `0` selects itself. It is also the earliest eligible start for index `1`. Index `2` selects the start at index `3`. ### Example 2 ```text Input: intervals = [[-4,-2],[-2,0],[0,0]] Output: [1,2,2] ``` ### Constraints - `1 <= intervals.length <= 20000`. - Each interval contains exactly two integers. - `-1000000 <= start_i <= end_i <= 1000000`. - All starting points are unique. ### Clarifications The comparison is against the current interval's end, and equality is allowed. A zero-length interval is valid and can be its own answer. Return original indices rather than sorted positions or endpoint values. Unique starts make the selected interval unique whenever one exists. ### What a Strong Answer Covers - The smallest eligible start and the inclusive endpoint comparison. - Original index preservation, negative endpoints, and no-answer cases. - Point intervals and permitted self-selection. - Correctness and resource bounds.

Overview: Practice finding the original index with the smallest start at or beyond each interval's end. Include negative coordinates, exact equality, and point intervals that select themselves.

Amazon logo
Amazon
Jul 10, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Find Each Interval's Nearest Eligible Starting Interval

You are given intervals [start_i,end_i] with unique starting points. For each interval i, find the original index j whose start is the smallest start satisfying start_j >= end_i.

Return these indices in an array aligned with the original interval order. Use -1 when no eligible start exists. An interval may select itself when its endpoints allow it.

Function

vector<int> findRightInterval(vector<vector<int>>& intervals)

Example 1

Input: intervals = [[4,4],[1,4],[6,8],[8,9]]
Output: [0,0,3,-1]

The point interval at index 0 selects itself. It is also the earliest eligible start for index 1. Index 2 selects the start at index 3.

Example 2

Input: intervals = [[-4,-2],[-2,0],[0,0]]
Output: [1,2,2]

Constraints

  • 1 <= intervals.length <= 20000 .
  • Each interval contains exactly two integers.
  • -1000000 <= start_i <= end_i <= 1000000 .
  • All starting points are unique.

Clarifications

The comparison is against the current interval's end, and equality is allowed. A zero-length interval is valid and can be its own answer. Return original indices rather than sorted positions or endpoint values. Unique starts make the selected interval unique whenever one exists.

What a Strong Answer Covers Guidance

  • The smallest eligible start and the inclusive endpoint comparison.
  • Original index preservation, negative endpoints, and no-answer cases.
  • Point intervals and permitted self-selection.
  • Correctness and resource bounds.