Challenge - 5 Problems
Two Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Two Sum solution?
Given the following TypeScript code for the Two Sum problem, what will be printed when calling twoSum([2, 7, 11, 15], 9)?
DSA Typescript
function twoSum(nums: number[], target: number): number[] {
const map = new Map<number, number>();
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 9 and their indices.
✗ Incorrect
The numbers 2 and 7 add up to 9. Their indices are 0 and 1 respectively, so the function returns [0, 1].
🧠 Conceptual
intermediateWhat is the time complexity of the classic Two Sum hash solution?
Consider the classic Two Sum solution using a hash map. What is the time complexity in Big O notation for this approach?
Attempts:
2 left
💡 Hint
Think about how many times the array is traversed and the cost of hash map operations.
✗ Incorrect
The solution traverses the array once (O(n)) and hash map lookups and insertions are O(1) on average, so total time complexity is O(n).
🔧 Debug
advancedWhat error does this Two Sum code produce?
Identify the error when running this TypeScript code snippet:
DSA Typescript
function twoSum(nums: number[], target: number): number[] {
const map = new Map<number, number>();
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 [];
}
twoSum([1, 2, 3], 4);Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop uses i <= nums.length which causes nums[nums.length] to be undefined. Subtracting from target with undefined leads to NaN and map.has(NaN) returns false, but accessing nums[i] later causes TypeError.
🚀 Application
advancedWhat is the output after running this Two Sum variant?
This variant returns the actual numbers instead of indices. What is printed when running this code?
DSA Typescript
function twoSumValues(nums: number[], target: number): number[] {
const map = new Map<number, boolean>();
for (const num of nums) {
const complement = target - num;
if (map.has(complement)) {
return [complement, num];
}
map.set(num, true);
}
return [];
}
console.log(twoSumValues([3, 1, 4, 6], 7));Attempts:
2 left
💡 Hint
Look at the order of numbers returned when complement is found.
✗ Incorrect
The function returns [complement, num] when complement is found. For input [3,1,4,6] and target 7, complement 3 is found when num is 4, so returns [3,4].
🧠 Conceptual
expertWhy is the classic Two Sum hash solution preferred over sorting and two-pointer approach for returning indices?
Choose the best reason why the classic hash map solution is preferred when the problem requires returning original indices of the two numbers.
Attempts:
2 left
💡 Hint
Think about what happens to indices when you sort the array.
✗ Incorrect
Sorting changes the order of elements, so original indices are lost. To return original indices, extra bookkeeping is needed, making the hash map approach simpler and more direct.
