Find the Unique Pair of Array Indices Whose Values Sum to a Target
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Given an integer array and a target, return the indices of the only pair of different elements whose values add up to the target, smaller index first. Arrays hold up to 10,000 values of up to one billion in magnitude, and the task is a common warm-up that tests clean, efficient array reasoning.
Constraints
- 2 <= len(nums) <= 10000
- -10^9 <= nums[i] <= 10^9
- -10^9 <= target <= 10^9
- Exactly one valid pair exists.
Examples
Input: ([2, 7, 11, 15], 9)
Expected Output: [0, 1]
Explanation: Source example 1: 2 + 7 = 9 at indices 0 and 1.
Input: ([3, 2, 4], 6)
Expected Output: [1, 2]
Explanation: Source example 2 and self-pairing trap: 3 is target/2 but appears once, so [0, 0] is invalid; 2 + 4 = 6.
Hints
- For any fixed index, the value its partner must hold is completely determined by target and the value at that index.
- With up to 10,000 elements, checking every pair is roughly 50 million comparisons. Look for a way to tell whether a needed value has already appeared without rescanning the array.
- When target is exactly twice some value, that value counts only if it appears at two different indices. The answer always lists the smaller index first.