1. Two Sum
easyAsked at SnowflakeGiven an array of integers and a target sum, return the indices of the two numbers that add up to target. Snowflake asks this as a warm-up to test if you can move from O(n^2) lookups to a hash-map index — the same mental jump a query planner makes when it picks a hash join over a nested-loop join.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2026-Q1)— Snowflake new-grad phone screens use this as the opening problem.
- LeetCode Discuss (2025-12)— Recurring at Snowflake SDE-I screens; interviewer wants the hash-map version.
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 loops
Try every pair (i, j) and check whether nums[i] + nums[j] == 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. This is the nested-loop join — fine on toy inputs, dies on 10^4 rows.
2. Hash map single pass (optimal)
Walk the array once; for each value v, check whether target - v is already in the map. If yes, return the indices; otherwise insert v -> index.
- 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: Linear time, linear space. This is the hash-join — build side fits in memory, probe side streams. Snowflake interviewers love the analogy.
Snowflake-specific tips
Snowflake interviewers grade this on how quickly you abandon the nested-loop solution and reach for a hash map. Bonus signal: relate it to query-planner choice between nested-loop join and hash join, and discuss when the build side wouldn't fit in memory (spilling to disk).
Common mistakes
- Checking the map BEFORE inserting the current element — but using == self-index lookups, which can produce false positives if you insert first.
- Forgetting that the problem allows negative numbers and very large values — using a fixed-size array as a 'hash' breaks here.
- Returning the values instead of the indices.
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- What if the array is sorted? (Two-pointer, O(1) space.)
- What if you need ALL pairs that sum to target, not just one?
- What if the array doesn't fit in memory — how would you partition it?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why insert AFTER the check?
If you insert first, then for an input like [3,3], target=6, you'd find a match against your own index. Inserting after the check guarantees the match is to an earlier element.
How does this map to Snowflake's query planner?
A hash join builds a hash table on the smaller input (the 'build side') and probes it with the larger input (the 'probe side'). Two Sum is the same pattern with build and probe interleaved into a single scan.
Practice these live with InterviewChamp.AI
Drill Two Sum and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →