Determine If Two Numbers Sum to Target
Company: LendingClub
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's basic algorithmic fluency in array processing and the ability to reason about pairwise sums using auxiliary data structures.
Constraints
- 0 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
- -2 * 10^9 <= target <= 2 * 10^9
- The two chosen elements must be at different indices (but may have equal values).
Examples
Input: ([2, 7, 11, 15], 9)
Expected Output: True
Explanation: 2 + 7 = 9, so a valid pair exists.
Input: ([3, 2, 4], 6)
Expected Output: True
Explanation: 2 + 4 = 6; note 3 + 3 is not valid because there is only one 3.
Input: ([3, 3], 6)
Expected Output: True
Explanation: The two 3s sit at different indices, so their sum 6 counts.
Input: ([1, 2, 3], 7)
Expected Output: False
Explanation: Maximum pair sum is 2 + 3 = 5, which is below 7.
Input: ([], 5)
Expected Output: False
Explanation: An empty array has no pair, so the answer is false.
Input: ([5], 5)
Expected Output: False
Explanation: A single element cannot form a pair with a distinct second element.
Input: ([-3, 4, 1, -1], 0)
Expected Output: True
Explanation: -1 + 1 = 0 (negatives and zero target are handled correctly).
Input: ([0, 0], 0)
Expected Output: True
Explanation: 0 + 0 = 0 using the two distinct zero entries.
Input: ([-5, -2, -3], -8)
Expected Output: True
Explanation: -5 + -3 = -8, a negative target satisfied by negative values.
Input: ([4, 4, 1], 9)
Expected Output: False
Explanation: Pairs sum to 8, 5, and 5 — none reaches 9, so the answer is false.
Hints
- Brute force checks every pair in O(n^2). Can you avoid re-scanning the array for each element?
- As you iterate, for the current value x the partner you need is target - x. If you have already seen that partner, you have a valid pair.
- Use a hash set of values seen so far. Check membership BEFORE inserting the current value so you never pair an element with itself.
- Alternative: sort the array (O(n log n)) and use two pointers from both ends, moving inward based on the running sum — trades time for O(1) extra space.