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
What is the output of the following C++ code that solves the Two Sum problem using a hash map?
DSA C++
#include <iostream> #include <vector> #include <unordered_map> std::vector<int> twoSum(const std::vector<int>& nums, int target) { std::unordered_map<int, int> hashMap; for (int i = 0; i < nums.size(); ++i) { int complement = target - nums[i]; if (hashMap.find(complement) != hashMap.end()) { return {hashMap[complement], i}; } hashMap[nums[i]] = i; } return {}; } int main() { std::vector<int> nums = {2, 7, 11, 15}; int target = 9; std::vector<int> result = twoSum(nums, target); for (int index : result) { std::cout << index << " "; } return 0; }
Attempts:
2 left
💡 Hint
Think about which two numbers add up to the target and their indices.
✗ Incorrect
The code finds two numbers in the vector that add up to the target 9. The numbers 2 and 7 at indices 0 and 1 add up to 9, so the output is '0 1 '.
🧠 Conceptual
intermediateUnderstanding Hash Map Usage in Two Sum
In the classic Two Sum hash solution, why do we check if the complement exists in the hash map before inserting the current number?
Attempts:
2 left
💡 Hint
Think about how the algorithm finds the pair efficiently in one pass.
✗ Incorrect
Checking for the complement before inserting the current number allows the algorithm to find the pair as soon as possible and prevents using the same element twice.
❓ Predict Output
advancedOutput with Duplicate Numbers in Two Sum
What is the output of this modified Two Sum code when the input vector contains duplicates?
DSA C++
#include <iostream> #include <vector> #include <unordered_map> std::vector<int> twoSum(const std::vector<int>& nums, int target) { std::unordered_map<int, int> hashMap; for (int i = 0; i < nums.size(); ++i) { int complement = target - nums[i]; if (hashMap.find(complement) != hashMap.end()) { return {hashMap[complement], i}; } hashMap[nums[i]] = i; } return {}; } int main() { std::vector<int> nums = {3, 3, 4, 5}; int target = 6; std::vector<int> result = twoSum(nums, target); for (int index : result) { std::cout << index << " "; } return 0; }
Attempts:
2 left
💡 Hint
Look for the first pair that sums to the target.
✗ Incorrect
The first two elements are both 3, and 3 + 3 = 6, so the output is indices 0 and 1.
🔧 Debug
advancedIdentify the Error in Two Sum Implementation
What error will this code produce when compiled and run?
DSA C++
#include <iostream> #include <vector> #include <unordered_map> std::vector<int> twoSum(const std::vector<int>& nums, int target) { std::unordered_map<int, int> hashMap; for (int i = 0; i <= nums.size(); ++i) { int complement = target - nums[i]; if (hashMap.find(complement) != hashMap.end()) { return {hashMap[complement], i}; } hashMap[nums[i]] = i; } return {}; } int main() { std::vector<int> nums = {1, 2, 3}; int target = 10; std::vector<int> result = twoSum(nums, target); for (int index : result) { std::cout << index << " "; } return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
✗ Incorrect
The loop uses i <= nums.size(), which causes an out-of-range access on the last iteration, leading to a runtime error.
🚀 Application
expertNumber of Unique Pairs Summing to Target
Given the Two Sum hash solution approach, how many unique pairs of indices (i, j) with i < j exist in the array nums = {1, 2, 3, 4, 3} that sum to target = 6?
Attempts:
2 left
💡 Hint
List all pairs that sum to 6 and count unique index pairs.
✗ Incorrect
Pairs that sum to 6 are (2,4) at indices (1,3) and (3,3) at indices (2,4). So total unique pairs are 2.
