DSA

Two Sum Interview Questions

What is Two Sum, and how do you solve it in O(n) time? — spoken sample answer for Indian interviews.

  • 5Questions with answers
  • 3Difficulty levels

Questions (5)

Browse beginner, intermediate, and advanced questions with answers — hide them when you want to self-test.

Question 1
Interview Beginner
Question

What is Two Sum, and how do you solve it in O(n) time?

Answer:

Given an array of numbers and a target, return the indices of two numbers that add up to the target. You cannot reuse the same element.

Check every pair with two nested loops. Complexity: Time O(n²) , Space O(1)

Use a hash map (value → index). For each nums[i], check if (target - nums[i]) exists in the map. If yes, return both indices; otherwise store nums[i] in the map. Complexity: Time O(n) , Space O(n)

If the array is sorted, use two pointers from both ends — O(n) time, O(1) space.

Empty array, no valid pair, return indices (not values) unless asked otherwise.

Question 2
Interview Beginner
Question

What if Two Sum must return the values instead of indices, and the array is sorted?

Answer:

If the array is sorted I would not use a hash map first. I would put two pointers at the left and right ends. If the sum is too small I move left up. If it is too big I move right down. That is O(n) time and O(1) extra space, and I return the two values. I still have to handle duplicates if they want unique pairs. The hash map is the unsorted interview default. Sorted plus values is the follow-up that checks whether I blindly copy the first solution.

Question 3
Interview Intermediate
Question

How do you handle duplicates in Two Sum if they want all unique pairs?

Answer:

I sort a copy if I am allowed, then two-pointer, and I skip the same value when I advance left or right so I do not emit (1, 5) twice. If I must keep indices and the original order, I still use a map from value to list of indices and I skip pairs I have already emitted using a set of sorted tuples. I would ask whether (2, 2) is allowed when 2 appears twice. That question saves me from a wrong assumption. Unique pairs is a different problem from the classic two-index version.

Question 4
Interview Intermediate
Question

Can you do Two Sum in one pass? What breaks if you insert into the map first?

Answer:

Yes, classic One-pass: for each i I look up target minus nums[i] before I insert nums[i]. If I insert first, a number can pair with itself when 2 * nums[i] equals target and there is only one copy. Example: [3, 3] target 6 is fine because the second 3 finds the first. [3] target 6 should fail. Lookup-then-insert is the safe order. I would mention that in the interview before they catch me.

Question 5
Interview Intermediate
Question

What is 3Sum, and how does it build on Two Sum?

Answer:

3Sum asks for triplets that add to zero, usually unique triplets. Brute force is O(n cubed) . The standard is sort, then for each i run two-pointer Two Sum on the rest. That is O(n squared) . I skip duplicate i and duplicate left/right values. I would not start 3Sum with a hash set of pairs in an interview unless they ask — two pointers after sort is what they want. Two Sum is the inner loop. If I cannot do Two Sum cleanly I will not do 3Sum.

Practice with AI mock interviews

Run DSA mock interviews with AI follow-ups, instant feedback, and analytics on AiLx.

Free to start · No credit card required