If you have ever searched for coding interview questions, chances are the first problem you encountered was Two Sum. This problem is often the entry point into the world of Data Structures and Algorithms and is asked in interviews at almost every company, from startups to FAANG.
Despite its simplicity, Two Sum tests a candidate’s understanding of arrays, hashing, and optimization techniques. In this article, we will break down the problem step by step and move from a brute-force approach to the optimal solution that interviewers expect.
Understanding the Problem
You are given an array of integers and a target value. Your task is to find two distinct numbers in the array such that their sum is equal to the given target.
You can assume that
Each input will have exactly one solution.
You cannot use the same element twice.
The order of the output does not matter.
Example
Input array: [2, 7, 11, 15]
Target: 9
Output: [0, 1]
This means that the elements at index 0 and index 1 add up to the target value.
Brute Force Approach
The most straightforward way to solve this problem is to check every possible pair of elements in the array and see if their sum equals the target.
How it works
Use two nested loops.
For each element, check every element after it.
If the sum matches the target, return their indices.
JavaScript Example
function twoSum(nums, target) {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}
}
}Why this approach is not ideal
Time complexity is O(n²).It becomes very slow for large arrays.
Interviewers usually expect a better solution.
Optimized Approach Using Hash Map
To optimize the solution, we need a way to check whether the required complementary value already exists while iterating through the array.
This is where hashing comes in.
Key idea
For every number x, we check if (target − x) already exists in a hash map.If it does, we have found our answer.
Steps
Create an empty map.
Traverse the array once.
For each element, calculate the required complement.
If the complement exists in the map, return the indices.
Otherwise, store the current number and its index in the map.
JavaScript Optimized Solution
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);}
}Dry Run of Optimized Approach
Input
Array = [2, 7, 11, 15]
Target = 9Step 1
Current number = 2
Complement = 7
Map = {}Store 2 → index 0
Step 2
Current number = 7
Complement = 2Map contains 2
Answer found → [0, 1]
This solution finds the result in a single pass.
Time and Space Complexity
Brute Force
Time Complexity: O(n²)
Space Complexity: O(1)Optimized Hash Map Approach
Time Complexity: O(n)
Space Complexity: O(n)This is why the hash map solution is preferred in interviews.
Why Two Sum Is So Important
Two Sum teaches several important concepts
How to trade space for time
How hashing improves performance
How to think in terms of complements instead of pairs
Many advanced problems are variations of Two Sum, such as
3Sum
4Sum
Pair Sum in Sorted Array
Count pairs with given sum
Common Mistakes Candidates Make
Using brute force without optimization
Forgetting that the same element cannot be used twice
Returning values instead of indices
Overcomplicating a simple problem
When to Use Which Approach
Use the brute-force approach only for learning or very small inputs.
Use the hash map approach in
Interviews
Competitive programming
Real-world applications
Final Thoughts
Two Sum is not just a beginner problem. It is a foundation problem. If you understand this problem deeply, you will find it much easier to solve a wide range of array and hashing problems.
Mastering Two Sum sets the tone for how you approach optimization in DSA.
Next problems to explore
Longest Substring Without Repeating Characters
Maximum Subarray (Kadane’s Algorithm)3Sum Problem
DSA with Piyush
Building strong fundamentals for coding interviews and beyond.