📖 Scenario: You are helping a friend who runs a small shop. They want to find two items from their price list that add up to a specific gift card amount. This will help customers quickly find pairs of items they can buy together.
🎯 Goal: Build a program that finds two numbers in a list that add up to a target sum using a fast method with a hash (object) to remember seen numbers.
📋 What You'll Learn
Create an array called prices with the exact values: [2, 7, 11, 15]
Create a variable called target and set it to 9
Use a for loop with variable i to go through prices
Use an object called seen to store numbers and their indices
Inside the loop, check if target - prices[i] is in seen
If found, save the pair of indices in an array called result
Print the result array
💡 Why This Matters
🌍 Real World
Finding pairs of items that sum to a gift card value helps customers shop efficiently and improves sales.
💼 Career
This problem teaches how to use hash maps (objects) for fast lookups, a common technique in software development and coding interviews.
Progress0 / 4 steps
1
Create the prices array
Create an array called prices with these exact values: [2, 7, 11, 15]
DSA Javascript
Hint
Use const prices = [2, 7, 11, 15]; to create the array.
2
Set the target sum
Create a variable called target and set it to 9
DSA Javascript
Hint
Use const target = 9; to set the target sum.
3
Find two numbers that add up to target
Create an empty object called seen and an empty array called result. Use a for loop with variable i to go through prices. Inside the loop, check if target - prices[i] is a key in seen. If yes, set result to an array with seen[target - prices[i]] and i, then break the loop. Otherwise, add prices[i] as a key to seen with value i.
DSA Javascript
Hint
Use an object to remember seen numbers and their indices. Check if the complement exists before adding the current number.
4
Print the result
Print the result array using console.log(result)
DSA Javascript
Hint
Use console.log(result); to show the indices of the two numbers.
Practice
(1/5)
1. What is the main purpose of using a hash map in the Two Sum problem classic solution?
easy
A. To store numbers and their indices for quick lookup
B. To sort the array before searching
C. To store the sum of all pairs
D. To reverse the array elements
Solution
Step 1: Understand the role of hash map
The hash map stores each number as a key and its index as the value for quick access.
Step 2: Explain why quick lookup is needed
Quick lookup helps find if the complement (target - current number) exists without scanning the entire array again.
Final Answer:
To store numbers and their indices for quick lookup -> Option A
Quick Check:
Hash map = quick index lookup [OK]
Hint: Hash map stores values for instant complement check [OK]
Common Mistakes:
Thinking hash map sorts the array
Storing sums instead of indices
Using hash map to reverse array
2. Which of the following is the correct syntax to check if a key exists in a JavaScript Map during the Two Sum solution?
easy
A. map.has(key)
B. map.contains(key)
C. map.exists(key)
D. map.includes(key)
Solution
Step 1: Recall JavaScript Map method for key check
The correct method to check if a key exists in a Map is has().
Step 2: Verify other options are invalid
Methods contains(), exists(), and includes() do not exist on Map objects.
Final Answer:
map.has(key) -> Option A
Quick Check:
Map key check = has() [OK]
Hint: Use map.has(key) to check existence [OK]
Common Mistakes:
Using contains() instead of has()
Using includes() which is for arrays
Trying to access keys directly
3. What will be the output of the following code snippet?
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));
medium
A. [2, 3]
B. [1, 2]
C. [0, 1]
D. []
Solution
Step 1: Trace the loop and map updates
At i=0, map is empty, store (2,0). At i=1, complement=9-7=2, map has 2 at index 0.
Step 2: Return indices when complement found
Return [0,1] as indices of numbers 2 and 7 that sum to 9.
Final Answer:
[0, 1] -> Option C
Quick Check:
Indices of 2 and 7 = [0, 1] [OK]
Hint: Return indices when complement found in map [OK]
Common Mistakes:
Returning values instead of indices
Not updating map before checking complement
Returning empty array incorrectly
4. Identify the bug in this Two Sum solution code:
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 [];
}
medium
A. Complement calculation is incorrect
B. map.set should be called before checking complement
C. Return statement should return values, not indices
D. Loop condition should be i < nums.length, not i <= nums.length
Solution
Step 1: Check loop boundary
The loop uses i <= nums.length which causes out-of-bounds access at last iteration.
Step 2: Correct loop condition
Change to i < nums.length to avoid accessing undefined element.
Final Answer:
Loop condition should be i < nums.length, not i <= nums.length -> Option D
Quick Check:
Loop boundary off-by-one error [OK]
Hint: Use i < length to avoid out-of-bounds error [OK]
Common Mistakes:
Using <= instead of < in loop
Swapping map.set and complement check order
Returning values instead of indices
5. Given the array nums = [3, 3, 4, 5] and target 6, what will the Two Sum classic hash solution return?
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 [];
}
hard
A. [1, 2]
B. [0, 1]
C. [2, 3]
D. []
Solution
Step 1: Trace the loop with duplicates
At i=0, map empty, store (3,0). At i=1, complement=6-3=3, map has 3 at index 0.