Finally master
technical interviews
with visual explanations

Your competitive edge in technical interviews.

Try it out for free

Check out a free lesson.

Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice.

Unprocessed
Current Number
Found Pair

Two Sum - Detailed Explanation

Solution Steps

  1. Initialize an empty hash map to store {number → index}
  2. For each number at index i:
  3. - Calculate complement = target - number
  4. - If complement exists in map, we found a solution
  5. - Otherwise, add current {number → i} to map

Key Insights

  • For each number x, we need to find y where x + y = target
  • This means y = target - x (the complement)
  • We can use a hash map to find complements quickly
  • Hash map provides O(1) lookup and insertion

Time & Space Complexity

Time
O(n) as we make one pass through the array
Space
O(n) to store at most n elements in the hash map

Edge Cases

  • array with 2 elements
  • no solution (not possible per constraints)
  • same element twice (not allowed)
Get your dream job

The ultimate technical interview prep resource. Feel confident in your next technical interview.

Improve your technical interviewing skills

Learn Leetcode Patterns

Build genuine algorithmic problem-solving skills. No memorization, actually understand the patterns and how they work.

Time is key

Save countless hours

No more blanking out at technical interviews. Learn efficiently with our explanations in TypeScript, Python, Java, C# and C++.

Become confident

Land your dream job

With the patterns and concepts you will learn, you won't be scared of technical interviews anymore.

No commitment

Check out our free lessons

We offer some free lessons to help you decide if the course is right for you.

Frequently asked questions

Have any questions?

What courses do you offer?
We currently offer courses for Leetcode interviews. We have Blind 75 and are currently working on Grind 75 and Neetcode 150.
How do I get access to the courses?
You pay a subscription (either monthly or yearly) and you get access to all of our courses.
What is the refund policy?
We offer free lessons for you to try out each course so we don't offer refunds. If you didn't like the courses, you can cancel your subscription at any time.
How do I cancel my subscription?
You can cancel your subscription at any time in your billing portal.
I cancelled my subscription, why is it still active?
You will be able to access all of our courses until the end of your billing period.