Challenge - 5 Problems
Two Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Two Sum Classic Hash Solution with distinct pairs
What is the output of this Go code that finds indices of two numbers adding up to target 9?
DSA Go
package main import "fmt" func twoSum(nums []int, target int) []int { hash := make(map[int]int) for i, num := range nums { complement := target - num if j, ok := hash[complement]; ok { return []int{j, i} } hash[num] = i } return nil } func main() { nums := []int{2, 7, 11, 15} target := 9 result := twoSum(nums, target) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Think about which two numbers add up to 9 and their indices in the slice.
✗ Incorrect
The function uses a map to store numbers and their indices. It finds that 2 (index 0) and 7 (index 1) add up to 9, so it returns [0 1].
❓ Predict Output
intermediateOutput when no two numbers sum to target
What does the Go code print when no two numbers add up to the target 10?
DSA Go
package main import "fmt" func twoSum(nums []int, target int) []int { hash := make(map[int]int) for i, num := range nums { complement := target - num if j, ok := hash[complement]; ok { return []int{j, i} } hash[num] = i } return nil } func main() { nums := []int{1, 2, 3, 4} target := 10 result := twoSum(nums, target) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Check if any two numbers in the slice add up to 10.
✗ Incorrect
No pair sums to 10, so the function returns nil, which prints as [].
🔧 Debug
advancedIdentify the runtime error in Two Sum implementation
What error does this Go code produce when run?
DSA Go
package main import "fmt" func twoSum(nums []int, target int) []int { hash := make(map[int]int) for i, num := range nums { complement := target - num if j, ok := hash[complement]; ok { return []int{j, i} } // Missing assignment to hash[num] } return nil } func main() { nums := []int{3, 2, 4} target := 6 result := twoSum(nums, target) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Check if the map is updated correctly inside the loop.
✗ Incorrect
The map is never updated with nums[i], so no complement is found. The function returns nil, which prints as but fmt.Println prints empty slice as [].
❓ Predict Output
advancedOutput of Two Sum with duplicate numbers
What is the output of this Go code when the input slice contains duplicates?
DSA Go
package main import "fmt" func twoSum(nums []int, target int) []int { hash := make(map[int]int) for i, num := range nums { complement := target - num if j, ok := hash[complement]; ok { return []int{j, i} } hash[num] = i } return nil } func main() { nums := []int{3, 3, 4, 5} target := 6 result := twoSum(nums, target) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Look for two numbers that add up to 6 and their earliest indices.
✗ Incorrect
The first 3 at index 0 and the second 3 at index 1 add up to 6, so output is [0 1].
🧠 Conceptual
expertWhy does the Two Sum hash solution return indices in order of first found pair?
Why does the classic Two Sum hash map solution return the indices of the first pair found that sums to the target?
Attempts:
2 left
💡 Hint
Think about the order of operations inside the loop.
✗ Incorrect
The function checks if the complement exists in the map before adding the current number, so the first pair found is returned immediately.
