Determine If Two Numbers Sum to Target
Company: LendingClub
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: 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.
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.