Challenge - 5 Problems
Two Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Two Sum Hash Map Solution
What is the output of the following Python code that solves the Two Sum problem using a hash map?
DSA Python
def two_sum(nums, target): lookup = {} for i, num in enumerate(nums): if target - num in lookup: return [lookup[target - num], i] lookup[num] = i result = two_sum([2, 7, 11, 15], 9) print(result)
Attempts:
2 left
💡 Hint
Think about which two numbers add up to the target 9 and their indices.
✗ Incorrect
The function finds two numbers that add up to 9. 2 (index 0) and 7 (index 1) sum to 9, so it returns [0, 1].
🧠 Conceptual
intermediate1:30remaining
Understanding Hash Map Usage in Two Sum
Why does the Two Sum hash map solution use a dictionary to store numbers and their indices?
Attempts:
2 left
💡 Hint
Think about how to find the other number needed to reach the target efficiently.
✗ Incorrect
The dictionary allows O(1) lookup to check if the complement exists, making the solution efficient.
❓ Predict Output
advanced2:00remaining
Output with No Valid Pair
What will be the output of this Two Sum hash map code when no two numbers add up to the target?
DSA Python
def two_sum(nums, target): lookup = {} for i, num in enumerate(nums): if target - num in lookup: return [lookup[target - num], i] lookup[num] = i return None result = two_sum([1, 2, 3, 4], 8) print(result)
Attempts:
2 left
💡 Hint
Check if any two numbers sum to 8 in the list.
✗ Incorrect
No two numbers in [1, 2, 3, 4] add up to 8, so the function returns None.
🔧 Debug
advanced2:00remaining
Identify the Error in Two Sum Code
What error does this code produce when run, and why?
DSA Python
def two_sum(nums, target): lookup = {} for i, num in enumerate(nums): if target - num in lookup: return [lookup[target - num], i] lookup[num] = i result = two_sum([3, 3], 6) print(result)
Attempts:
2 left
💡 Hint
Check how the function handles duplicates and if the lookup dictionary is accessed safely.
✗ Incorrect
The code correctly finds indices [0, 1] because 3 + 3 = 6 and both indices are stored properly.
🚀 Application
expert3:00remaining
Number of Unique Pairs Summing to Target
Given the list [1, 2, 3, 4, 3, 2, 1] and target 4, how many unique pairs of indices (i, j) with i < j exist such that nums[i] + nums[j] == target?
Attempts:
2 left
💡 Hint
Consider pairs like (0,2), (1,5), and count unique index pairs carefully.
✗ Incorrect
Pairs are (0,2), (0,4), (1,5), (2,6), (4,6). Total 5 unique pairs.