🏷️ Category: HashMap / Algorithms  |  🎯 Difficulty: Easy  |  πŸ”₯ Frequency: β˜…β˜…β˜…β˜…β˜…


πŸ“‹ Problem Statement

Given a list of integers and a target, return the indices of two numbers that add up to target using the concept of Two Sum.

πŸ“₯ Input  β†’ nums = [2, 7, 11, 15],  target = 9
πŸ“€ Output β†’ [0, 1]    # 2 + 7 = 9

πŸ“₯ Input  β†’ nums = [3, 2, 4],  target = 6
πŸ“€ Output β†’ [1, 2]    # 2 + 4 = 6

πŸ“₯ Input  β†’ nums = [3, 3],  target = 6
πŸ“€ Output β†’ [0, 1]

🧠 What the Interviewer is Testing

ConceptWhy It Matters
HashMap usageCore data structure skill
O(n) thinkingOptimization over brute force
Complement logicMathematical reasoning

⚠️ Warning: Never start with O(nΒ²) brute force. Go straight to the hashmap solution.


πŸ’‘ Approach

a + b = target  β†’  b = target - a  (complement)

For every number, check if its complement already exists in the hashmap.
If yes β†’ found the pair. If no β†’ store current number and index.

βœ… Optimal Solution

def two_sum(nums: list, target: int) -> list:
    seen = {}  # { number: index }

    for i, num in enumerate(nums):
        complement = target - num

        if complement in seen:
            return [seen[complement], i]

        seen[num] = i

    return []

# ── Test Cases ─────────────────────────────────
print(two_sum([2, 7, 11, 15], 9))   # βœ… [0, 1]
print(two_sum([3, 2, 4], 6))        # βœ… [1, 2]
print(two_sum([3, 3], 6))           # βœ… [0, 1]

πŸ” Step-by-Step Walkthrough

nums = [2, 7, 11, 15]target = 9:

StepinumcomplementseenAction
1027{}Store 2β†’0
2172{2:0}βœ… Found! Return [0,1]

πŸ“Š Complexity Analysis

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Approach         β”‚ Time       β”‚ Space                    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Brute Force      β”‚ O(nΒ²)      β”‚ O(1)                     β”‚
β”‚ HashMap βœ…       β”‚ O(n)       β”‚ O(n)                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🎀 How to Explain in the Interview

β€œI’ll use a hashmap. For each number I calculate its complement β€” target minus current number. If that complement already exists in the map, I return both indices. Otherwise I store the current number. This gives O(n) time at the cost of O(n) space.”

β€œI’ll use a hashmap. For each number I calculate its complement β€” target minus current number. If that complement already exists in the map, I return both indices. Otherwise I store the current number. This gives O(n) time at the cost of O(n) space.”


πŸ” Common Follow-up Questions

Q1 β†’ Multiple valid pairs?

results = []
for i, num in enumerate(nums):
    complement = target - num
    if complement in seen:
        results.append([seen[complement], i])
    seen[num] = i
return results

Q2 β†’ Numbers can be negative?

Same solution works β€” hashmap handles negatives fine.

πŸ“ Set 1 of 4 β€” Python Interview Prep Series β€” devprepguide.com

Scroll to Top