Solving Two Sum and Trickier Variations
This post is part of a series on how to effectively solve entire classes of technical interview questions.
Rather than focusing on rote memorization of solutions, this series emphasizes patterns and ideas that you can reuse across wide swathes of coding questions that are asked by FAANG companies, elite trading firms, and top startups.
Introduction
Two Sum is a classic interview question and asked by major tech companies such as Google, Microsoft, Facebook, Amazon, Adobe, and Apple.
It’s a good litmus test to see if a candidate knows the basics of coding (and a good reason why it’s the most popular question on Leetcode). There are multiple approaches to solve it, and it’s a useful exercise to evaluate how the candidate weighs the pros and cons of each. I’ve asked this question in interviews too; if a candidate can’t solve it quickly (within 5–10 minutes), I know I won’t be moving them forward in the interview process.
Because it’s an “easier” problem, companies often ask harder variations:
- Allowing / disallowing duplicates in the input and output
- Three sum, Four sum, N-sum
- Different memory and time complexity requirements