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 JavaScript code that solves the Two Sum problem using a hash map?
DSA Javascript
function twoSum(nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (map.has(complement)) {
return [map.get(complement), i];
}
map.set(nums[i], i);
}
return [];
}
console.log(twoSum([2, 7, 11, 15], 9));Attempts:
2 left
💡 Hint
Think about which two numbers add up to the target 9.
✗ Incorrect
The numbers 2 and 7 at indices 0 and 1 add up to 9, so the function returns [0, 1].
❓ Predict Output
intermediateOutput when no two numbers sum to target
What is the output of this Two Sum hash solution when no two numbers add up to the target?
DSA Javascript
function twoSum(nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (map.has(complement)) {
return [map.get(complement), i];
}
map.set(nums[i], i);
}
return [];
}
console.log(twoSum([1, 2, 3, 4], 10));Attempts:
2 left
💡 Hint
Check if any two numbers add up to 10.
✗ Incorrect
No two numbers in the array add up to 10, so the function returns an empty array.
🧠 Conceptual
advancedWhy use a hash map in Two Sum?
Why does the classic Two Sum solution use a hash map instead of nested loops?
Attempts:
2 left
💡 Hint
Think about how quickly you can find if a number exists using a hash map.
✗ Incorrect
Using a hash map allows checking if the complement exists in constant time, reducing overall time complexity.
❓ Predict Output
advancedOutput with duplicate numbers in input
What is the output of this Two Sum hash solution when the input array has duplicates?
DSA Javascript
function twoSum(nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (map.has(complement)) {
return [map.get(complement), i];
}
map.set(nums[i], i);
}
return [];
}
console.log(twoSum([3, 3, 4, 5], 6));Attempts:
2 left
💡 Hint
Look for two numbers that add up to 6, even if they are the same number.
✗ Incorrect
The two 3's at indices 0 and 1 add up to 6, so the function returns [0, 1].
🔧 Debug
expertIdentify the error in Two Sum hash solution variant
What error will this code produce when run, and why?
DSA Javascript
function twoSum(nums, target) {
const map = {};
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (map[complement] !== undefined) {
return [map[complement], i];
}
map[nums[i]] = i;
}
return [];
}
console.log(twoSum([0, 4, 3, 0], 0));Attempts:
2 left
💡 Hint
Check how the map object treats keys with value 0.
✗ Incorrect
Because map[0] is 0, which is falsy, the original if condition fails and the complement is not detected, causing the function to return []. Changing the condition to check for undefined fixes this.
