1. Two Sum
easyAsked at SquareGiven an array of integers and a target, return indices of two numbers that add up to the target. Square asks this as a warm-up to gauge whether you reach for hash maps the moment lookup-by-value enters the picture — a habit that scales into their transaction-matching code.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Square loops.
- LeetCode Discuss (2026-Q1)— Square phone screen warm-up; expect the interviewer to push for one-pass after you give two-pass.
- Glassdoor (2025)— Cash App backend phone screens cite this as the opening question.
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 each input has exactly one solution, and you may not use the same element twice. Return the answer in any order.
Constraints
2 <= nums.length <= 10^4-10^9 <= nums[i] <= 10^9Exactly one valid answer exists.
Examples
Example 1
nums = [2,7,11,15], target = 9[0,1]Explanation: nums[0] + nums[1] == 9.
Example 2
nums = [3,2,4], target = 6[1,2]Approaches
1. Brute force pair scan
Try every pair (i, j) with i < j and check if their sum 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 — fine for n=10 but Square interviewers will probe what happens at the 10^4 ceiling.
2. Hash map single-pass
Walk once; for each value v at index i, check if (target - v) is already in the map. If yes return both indices, else store v -> i.
- 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: Single pass, O(n) time. The classic trick: store-then-look becomes look-then-store so you never match an index with itself.
Square-specific tips
Square interviewers grade you on whether you state the invariant ('each value at i looks for target - nums[i] among already-seen values') before coding. Mention idempotency naturally — Square's payments stack rewards candidates who think about repeated lookups as a map-backed pattern, not a loop.
Common mistakes
- Storing the value before checking, which lets a single element pair with itself when nums[i] == target/2.
- Returning values instead of indices (the spec asks for indices).
- Using an object {} and tripping over numeric keys that JavaScript stringifies — prefer Map.
Follow-up questions
An interviewer at Square may pivot to one of these next:
- What if the array is sorted? (Two-pointer in O(1) space.)
- What if there can be multiple valid pairs and you must return all of them?
- How would you handle this on a streaming POS feed where the array doesn't fit in memory?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why Map over a plain object?
Map preserves numeric key types and has O(1) get/set. Plain objects coerce keys to strings and can collide on inherited keys like 'constructor'.
What if there's no solution?
The problem guarantees one exists. In a production POS context you would return null or throw and let the caller decide — Square interviewers like that nuance.
Practice these live with InterviewChamp.AI
Drill Two Sum and other Square interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →