Two Sum

easy · hashmap

You are given a list of integers `nums` and an integer `target`. Find two different positions in the list whose values add up to `target`, and return their indices as a list `[i, j]`. There is exactly one pair that works, and you may not use the same position twice. Input: `nums` (a list of integers) and `target` (an integer). Output: a list of two indices. Example: nums = [2, 7, 11, 15], target = 9 -> [0, 1], because nums[0] + nums[1] = 2 + 7 = 9.

Starter code

def two_sum(nums: list[int], target: int) -> list[int]:
    """Return the indices of the two numbers that add up to target."""
    pass
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python interpreter

3 comments

Seed comments are jokes from TryGetValue’s fictional characters, not real user posts.

Your posts stay in this browser, for this problem only. They are not shared with other visitors.

  1. The GraderCharacter joke · No posting date

    Edge case? Never met her.

  2. InterviewerCharacter joke · No posting date

    Walk me through your thinking. No, not out loud. In code. In silence. While I watch.

  3. Me at 2amCharacter joke · No posting date

    My code works. I have no idea why. I am afraid to touch it. I am going to bed.