Given an array of integers and a target value, find two numbers in the array that add up to the target. Return their indices.
For example, given nums = [2, 7, 11, 15] and target = 9, the output is [0, 1] because nums[0] + nums[1] = 2 + 7 = 9.
Explain your approach, walk through how it works, and analyze the time and space complexity. Discuss what edge cases you would consider.
How to approach it
- Hint 1
For each number, think about what value you need to find to reach the target.
- Hint 2
A data structure that supports O(1) lookups could help you avoid checking every pair.
- Hint 3
A hash map lets you store each number's index as you iterate -- check if the complement (target - current) already exists.
Ready to answer it out loud?
Record your answer in 1:00 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.