Day 9 of 90EasyLeetCode

Two Sum

Given a list of numbers and a target, return the positions of the two numbers that add up to the target.

Pattern
Hash map lookup
Time box
15 min
Time
O(n)
Space
O(n)

Try it first — give it 15 minutes

0/4 revealed

Stuck? Take one hint at a time. The struggle is where the pattern sticks.

    The problem in plain English

    You get an array nums and a number target. Exactly two numbers in the array add up to target. Return their indices (positions), in any order. You can't use the same element twice, but two different positions can hold the same value.

    Example: nums = [2, 7, 11, 15], target = 9 → [0, 1], because nums[0] + nums[1] = 2 + 7 = 9.

    Start with brute force

    Try every pair. For each i, check every j > i and see if nums[i] + nums[j] == target.

    It's correct, and it's worth saying out loud in an interview, but with n numbers that's about n²/2 pair checks. For n = 10,000 that's 50 million checks. We can do far better.

    Building the intuition

    Look at the brute force again. When you're standing on nums[i] = 2 with target = 9, you're not really searching for any number. You're searching for one specific number: 9 − 2 = 7.

    So the real question for every element is: "Have I already seen target − x?"

    A list answers that in O(n). A hash map answers it in O(1). That's the whole trick.

    The optimal approach

    1. Create an empty hash map seen that stores value → index.
    2. Walk the array with index i and value x.
    3. Compute need = target − x.
    4. If need is in seen, you're done: return [seen[need], i].
    5. Otherwise store seen[x] = i and keep going.

    Why check before storing? So an element never pairs with itself. With nums = [3, 3], target = 6: at i = 0 the map is empty, so we store 3 → 0. At i = 1 we need 3, find it at index 0, and return [0, 1]. Correct.

    The code

    Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.

    class Solution:
        def twoSum(self, nums: List[int], target: int) -> List[int]:
            seen = {}  # value -> index of every number we've passed
    
            for i, x in enumerate(nums):
                need = target - x
                if need in seen:          # partner already seen?
                    return [seen[need], i]
                seen[x] = i               # store AFTER checking, so x can't pair with itself
    
            return []  # the problem guarantees an answer, so we never get here

    Dry run

    nums = [2, 7, 11, 15], target = 9

    ixneed = 9 − xseen before the checkFound?
    027{}No → store 2 → 0
    172{2: 0}Yes → return [0, 1]

    We stopped after two steps instead of checking every pair.

    Complexity

    • Time: O(n). One pass, and each map lookup and insert is O(1) on average.
    • Space: O(n). In the worst case (the pair is at the very end) the map holds almost every number.

    This is the classic time–space trade-off: we spend O(n) memory to go from O(n²) to O(n) time.

    Edge cases and traps

    • Duplicates like [3, 3], target 6. Works because we look up before inserting.
    • Negative numbers and zero. target − x still works; nothing assumes positivity.
    • Returning values instead of indices. Read the question: it asks for positions.
    • Sorting first. Sorting destroys the original indices. The two-pointer version (see Two Sum II) only fits when the input is already sorted.

    Say it in the interview

    "Brute force checks every pair, which is O(n²). But for each number I know exactly which partner I need, target − x. If I keep a hash map from value to index for everything I've already passed, I can check for the partner in O(1). One pass, O(n) time and O(n) space. I check before inserting so an element can't pair with itself."

    Follow-ups interviewers ask

    • "What if the array is sorted?" Use two pointers from both ends for O(1) extra space — you will meet this as Two Sum II in the Two Pointers stage.
    • "What if you need all pairs, not one?" Keep counts in the map and watch out for double-counting.
    • "What if memory is very tight?" Sort a copy of (value, index) pairs and use two pointers: O(n log n) time, but no hash map.

    Practise the same idea

    This is Day 9 of a free 90-day plan 🐸

    90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.

    Get the full roadmap — free