Find two numbers summing to target
Company: Aeonea
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates algorithm design and complexity analysis skills, specifically the ability to identify element pairs in arrays and reason about time-space tradeoffs.
Constraints
- 0 <= len(nums) <= 200000
- -10^9 <= nums[i] <= 10^9
- -10^9 <= target <= 10^9
- Return indices i < j of two distinct elements
- If no valid pair exists, return []
- Expected solution: O(n) time, O(n) extra space
Hints
- Use a hash map from value to its earliest index seen so far.
- For each index i and value x, check if target - x is already in the map; if so, return [map[target - x], i].
- Store an index for a value only once to keep the earliest occurrence; this handles duplicates correctly.
- If you finish scanning without a match, return []. Negative numbers work the same way.