โ† 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.