📖 Scenario: You are helping a friend who runs a small store. 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 hash map (object) for fast lookup.
📋 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 iterate over prices
Inside the loop, use a hash map object called seen to check complements
Print the indices of the two numbers that add up to target as an array
💡 Why This Matters
🌍 Real World
Finding two items that sum to a gift card value helps customers shop quickly and improves store sales.
💼 Career
This problem is a classic example of using hash maps for fast lookup, a key skill 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 Typescript
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 Typescript
Hint
Use const target = 9; to set the target sum.
3
Find two indices using a hash map
Create an empty object called seen. Use a for loop with variable i to iterate over prices. Inside the loop, calculate complement = target - prices[i]. If complement is a key in seen, break the loop. Otherwise, add prices[i] as a key with value i to seen.
DSA Typescript
Hint
Use an object to store seen numbers and their indices. Check if complement exists before adding current number.
4
Print the result
Print the result array using console.log(result)
DSA Typescript
Hint
Use console.log(result); to show the indices.
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 the sum of all pairs
B. To sort the array before searching
C. To store numbers and their indices for quick lookup
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 lookup.
Step 2: Explain why quick lookup is needed
Quick lookup helps to 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 C
Quick Check:
Hash map stores number:index = A [OK]
Hint: Hash map stores values for instant complement check [OK]
Common Mistakes:
Thinking hash map sorts the array
Trying to store sums instead of indices
Using hash map to reverse elements
2. Which of the following TypeScript code snippets correctly initializes an empty hash map for the Two Sum problem?
easy
A. const map = new Map();
B. const map = {};
C. const map = new Array();
D. const map = new Set();
Solution
Step 1: Identify correct hash map initialization
In TypeScript, new Map<number, number>() creates a map with number keys and values.
Step 2: Explain why other options are incorrect
{} is an object but lacks type safety; new Array() is an array, not a map; new Set() stores unique values, not key-value pairs.
Final Answer:
const map = new Map<number, number>(); -> Option A
Quick Check:
Use Map<number, number> for hash map [OK]
Hint: Use Map<number, number> for key-value pairs [OK]
Common Mistakes:
Using Set instead of Map
Using Array instead of Map
Using plain object without type safety
3. What will be the output of the following TypeScript code for the input array [2, 7, 11, 15] and target 9?
function twoSum(nums: number[], target: number): number[] {
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));