← Core Algorithms
#1 most askedVery commonEasyHash map

Two Sum

New to Hash map? Learn it from scratch first
Hash Maps & Sets β†’
🏒 Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.
I was asked this

Given a list of numbers nums and a number target, return the indices of the two numbers that add up to target. Each input has exactly one answer, and you can't use the same element twice.

two_sum([2, 7, 11, 15], 9) β†’ [0, 1] because 2 + 7 = 9.

πŸ§ͺ Practice

Write your solution, then press Run tests. Explain your approach out loud as you go.

Python
def two_sum(nums, target):
    pass

Output appears here. Press Run or Ctrl/⌘ + Enter.

Try it first. Struggling for a few minutes is how it sticks.