Welcome to the first post in our series on tackling LeetCode problems with TypeScript! If you’re preparing for technical interviews in Silicon Valley, you’re likely aware that mastering data structures and algorithms is crucial. LeetCode offers a fantastic platform to hone these skills, and in this series, we’ll solve problems ranging from elementary to advanced, all while leveraging TypeScript’s powerful type system to write clean, maintainable code.
In this post, we’re starting with one of the most iconic and frequently asked interview questions: Two Sum. This problem is often used as a warm-up question at companies like Amazon and Google, testing your ability to manipulate arrays and optimize solutions using hash maps. Let’s dive in and explore how to solve it step by step with TypeScript.
The Two Sum problem is straightforward but packs a punch in terms of teaching core concepts. Here’s the problem statement:
nums
and an integer target
, return the indices of the two numbers such that they add up to target
. You may assume that each input would have exactly one valid solution, and you may not use the same element twice.nums = [2, 7, 11, 15]
, target = 9
[0, 1]
(because nums[0] + nums[1] = 2 + 7 = 9
)The key concepts we’ll cover include arrays, hash maps, and time complexity analysis, aiming for an efficient solution with time complexity.
Imagine you have a box of toy blocks, each with a number written on it. Your mom gives you a special number, say 9, and asks you to find two blocks that add up to that number. You also need to tell her where those blocks are in the box (their positions).
The fast way is like using a hash map—a special tool to remember numbers and their positions quickly. This makes finding the two blocks much faster, even if you have a huge box of blocks!
Let’s implement the Two Sum solution in TypeScript. We’ll use a hash map approach to achieve time complexity, where is the length of the input array. TypeScript’s type system will help us define the input and output types clearly, making our code more robust.
Here’s the complete TypeScript solution with detailed comments:
function twoSum(nums: number[], target: number): number[] {
// Create a hash map to store number-to-index mappings
const numMap: Map<number, number> = new Map();
// Iterate through the array
for (let i = 0; i < nums.length; i++) {
const currentNum = nums[i];
// Calculate the complement needed to reach the target
const complement = target - currentNum;
// Check if complement exists in the map
if (numMap.has(complement)) {
// Return the indices of the two numbers
return [numMap.get(complement)!, i];
}
// If not found, add the current number and its index to the map
numMap.set(currentNum, i);
}
// No solution found (though problem guarantees one exists)
return [];
}
// Test the function
const nums = [2, 7, 11, 15];
const target = 9;
console.log(twoSum(nums, target)); // Output: [0, 1]
nums
as number[]
and the output as number[]
(an array of two indices). This ensures TypeScript catches any type mismatches during development.Map<number, number>
to store numbers as keys and their indices as values. Map
is a built-in TypeScript/JavaScript data structure that’s perfect for quick lookups.In an interview setting, starting with a brute-force approach (nested loops, time) is fine, but you should quickly pivot to the hash map solution to demonstrate optimization skills. Be prepared to explain edge cases, like:
Sometimes, interviewers might tweak the problem to ask for all pairs of indices that sum to the target (not just one pair). Let’s solve this variation in TypeScript as an additional exercise to deepen our understanding.
nums
and an integer target
, return all pairs of indices where the numbers add up to target
. Numbers can be used only once per pair, but the array might have duplicates.function twoSumAllPairs(nums: number[], target: number): number[][] {
// Create a hash map to store number-to-indices mappings (handling duplicates)
const numMap: Map<number, number[]> = new Map();
const result: number[][] = [];
// Populate the map with all indices for each number
for (let i = 0; i < nums.length; i++) {
const currentNum = nums[i];
if (!numMap.has(currentNum)) {
numMap.set(currentNum, []);
}
numMap.get(currentNum)!.push(i);
}
// Iterate through the array to find pairs
for (let i = 0; i < nums.length; i++) {
const currentNum = nums[i];
const complement = target - currentNum;
// Check if complement exists
if (numMap.has(complement)) {
const complementIndices = numMap.get(complement)!;
// Add pairs, ensuring we don't reuse the same index
for (const j of complementIndices) {
if (j > i) { // Only consider indices after i to avoid duplicates
result.push([i, j]);
}
}
}
}
return result;
}
// Test the function
const numsVariation = [1, 5, 5, 1, 3];
const targetVariation = 6;
console.log(twoSumAllPairs(numsVariation, targetVariation)); // Output: [[0, 1], [0, 2], [3, 1], [3, 2]]
j > i
to avoid using the same index twice in a pair and to prevent duplicate pairs in the result.This variation shows how to adapt the original solution to a slightly different requirement, a common scenario in interviews where follow-up questions test your flexibility.
In this blog post, we’ve tackled the classic LeetCode problem Two Sum, a staple in Silicon Valley technical interviews at companies like Amazon and Google. We started with an introduction to the problem, broke it down with an ELI5 analogy of finding toy blocks, and implemented a solution in TypeScript using a hash map for time complexity. We also explored a variation to find all possible pairs, showcasing how to handle follow-up questions.
Key takeaways:
Map
in TypeScript) to optimize array problems from to time.In the next post, we’ll build on these concepts with another easy problem, likely focusing on strings or linked lists. Stay tuned, and happy coding! If you have questions or want to see another variation, drop a comment below.