1. Two Sum
easyAsked at SnapGiven an array of integers and a target, return the indices of the two numbers that add up to the target. Snap uses this warm-up to gauge if you instinctively reach for a hash-map cache versus brute-forcing the lookup.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snap loops.
- Glassdoor (2026-Q1)— Snap phone-screen reports cite this as the standard warm-up problem before harder follow-ups.
- LeetCode Discuss (2025)— Frequently the first 5-minute screen at Snap before pivoting to streaming/cache problems.
Problem
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.
Constraints
2 <= nums.length <= 10^4-10^9 <= nums[i] <= 10^9-10^9 <= target <= 10^9Only one valid answer exists.
Examples
Example 1
nums = [2,7,11,15], target = 9[0,1]Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2
nums = [3,2,4], target = 6[1,2]Approaches
1. Brute force nested loop
For each index i, scan every j > i and check if nums[i] + nums[j] equals target.
- Time
- O(n^2)
- Space
- O(1)
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];
}
}
return [];
}Tradeoff: Quadratic time. Acceptable as a starting point only; Snap interviewers expect you to immediately recognize the cache opportunity.
2. Hash-map single pass (optimal)
Iterate once. For each value v at index i, check whether target - v is already in the map; if so, return the stored index and i. Otherwise store v.
- Time
- O(n)
- Space
- O(n)
function twoSum(nums, target) {
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const need = target - nums[i];
if (seen.has(need)) return [seen.get(need), i];
seen.set(nums[i], i);
}
return [];
}Tradeoff: Trades O(n) memory for O(n) time. The standard linear solution all Snap candidates are expected to produce.
Snap-specific tips
Snap's bar at the phone-screen is communication speed: state the complexity tradeoff in one breath ('O(n^2) brute, O(n) with a hash map for O(n) extra space'), then code the optimal solution. Bonus signal at Snap is naming the trade as a cache pattern, which they reuse for image/media lookups across the app.
Common mistakes
- Storing values into the map before checking for the complement — you can match nums[i] against itself when target == 2 * nums[i].
- Returning the values instead of the indices — re-read the prompt before coding.
- Using indexOf/lastIndexOf inside a loop, silently re-introducing O(n^2).
Follow-up questions
An interviewer at Snap may pivot to one of these next:
- What if the input array is sorted? (Use two pointers, O(n) time, O(1) space.)
- What if multiple answers exist — return all distinct pairs.
- Extend to Three Sum / K Sum.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Should I sort first and use two pointers?
Only if the problem allows returning values instead of indices. Sorting destroys the original indices, so for Two Sum's exact prompt the hash map is cleaner.
Is the hash-map solution stable for duplicates?
Yes — the map stores the first occurrence and the lookup happens before overwriting, so the returned pair is always the earliest matching index pair.
Practice these live with InterviewChamp.AI
Drill Two Sum and other Snap interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →