Return the Lexicographically Smallest Two-Sum Index Pair
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Find the lexicographically smallest pair of distinct indices whose array values sum to a target. Success depends on deterministic tie handling, duplicates, missing-result behavior, nonmutation, hash-based performance, and comparison with sorting-based alternatives.
Read the full Google Software Engineer interview experience this question came from
Constraints
- 0 <= nums.length <= 200,000
- -1,000,000,000 <= nums[i] <= 1,000,000,000
- -1,000,000,000 <= target <= 1,000,000,000
- Pair sums and complements reach +/-2,000,000,000; use signed 64-bit arithmetic for sums (long in Java, long long in C++)
- nums must not be mutated by the solution
- Target complexity: O(n) time and O(n) auxiliary space
Examples
Input: ([], 6)
Expected Output: []
Input: ([5], 5)
Expected Output: []
Hints
- The pair with the smallest right index is not the same as the pair with the smallest left index. Decide which index the required order minimizes first, then drive your scan by that index.
- Fix a left index i. What you actually need is the smallest position j > i whose value equals target - nums[i]. Knowing every position at which each value occurs makes that question answerable without rescanning the suffix.
- Positions of equal values arrive in increasing order, so a single cursor per value can stay pointed at the first occurrence beyond the index you are currently considering, keeping the whole scan linear.