Two Sum - Problem
The Two Sum problem is a fundamental coding challenge that appears in countless technical interviews! ๐ŸŽฏ

Imagine you're shopping with a specific budget. You have a list of item prices and need to find exactly two items that together cost your target amount. That's essentially what this problem asks you to do with numbers!

The Challenge:
Given an array of integers nums and an integer target, find the indices of two numbers that add up to the target value.

Key Rules:
โ€ข Each input has exactly one solution - no more, no less
โ€ข You cannot use the same array element twice
โ€ข Return the indices in any order

Example: If nums = [2,7,11,15] and target = 9, then nums[0] + nums[1] = 2 + 7 = 9, so return [0,1].

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [2,7,11,15], target = 9
โ€บ Output: [0,1]
๐Ÿ’ก Note: Because nums[0] + nums[1] = 2 + 7 = 9, we return [0, 1]. This is the most straightforward case where the first two elements sum to the target.
example_2.py โ€” Target at End
$ Input: nums = [3,2,4], target = 6
โ€บ Output: [1,2]
๐Ÿ’ก Note: nums[1] + nums[2] = 2 + 4 = 6. Note that we cannot use nums[0] + nums[0] = 3 + 3 = 6 because we cannot use the same element twice.
example_3.py โ€” Minimum Input
$ Input: nums = [3,3], target = 6
โ€บ Output: [0,1]
๐Ÿ’ก Note: The only solution is to use both elements: nums[0] + nums[1] = 3 + 3 = 6. This demonstrates the minimum possible input size.

Visualization

Tap to expand
๐ŸŽฏ Two Sum: The Party Problem2Position 07Position 111Position 215Position 3๐Ÿ“ Notebook2 โ†’ pos 0Looking for: 7Found 7!9 - 7 = 2 โœ“๐ŸŽ‰ Solution Found!Person at position 0+ Person at position 1Target: 9 | Answer: [0, 1]
Understanding the Visualization
1
The Brute Force Way
Talk to each person and ask every other person if their numbers sum to the target. Very thorough but exhausting!
2
The Smart Way - Hash Table
As you meet each person, write their number and location in your notebook. For each new person, quickly check if you need their 'complement' number.
3
The Moment of Truth
When you meet someone whose complement you've already seen, you've found your pair! Return both locations.
4
Mission Complete
You've efficiently found the two indices that sum to your target in just one pass through the party!
Key Takeaway
๐ŸŽฏ Key Insight: Instead of checking every possible pair (O(nยฒ)), use a hash table to remember what you've seen and instantly find what you need (O(n))!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array. Each hash map operation (lookup and insert) takes O(1) average time, so total is O(n)

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Hash map stores at most n key-value pairs in worst case (when solution is the last pair checked)

n
2n
โšก Linearithmic Space

Constraints

  • 2 โ‰ค nums.length โ‰ค 104
  • -109 โ‰ค nums[i] โ‰ค 109
  • -109 โ‰ค target โ‰ค 109
  • Only one valid answer exists
Asked in
Google 152 Amazon 98 Meta 87 Microsoft 73
142.4K Views
Very High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen