1. Two Sum
easyAsked at GleanGlean uses this as a warm-up to test whether candidates think in hash maps first — the same O(1) lookup pattern that powers their inverted-index search engine at the core.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Glean loops.
- Glassdoor (2026-Q1)— Cited in Glean SWE phone-screen reports as a standard warm-up.
- Blind (2025-10)— Multiple Glean threads confirm Two Sum appears in early screening rounds to filter for hash-map fluency.
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: nums[0] + nums[1] = 2 + 7 = 9.
Example 2
nums = [3,2,4], target = 6[1,2]Explanation: nums[1] + nums[2] = 2 + 4 = 6.
Approaches
1. Brute force
Try every pair of indices. For each i, scan every j > i and check if nums[i] + nums[j] == target.
- Time
- O(n²)
- 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: Simple to code but too slow for large inputs. Mention it first, then immediately propose the hash-map approach.
2. Hash map (one pass)
Store each number's index in a map as you iterate. For each num, check if (target - num) is already in the map. If yes, you found the pair.
- Time
- O(n)
- Space
- O(n)
function twoSum(nums, target) {
const seen = new Map(); // value -> index
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
return [seen.get(complement), i];
}
seen.set(nums[i], i);
}
return [];
}Tradeoff: O(n) time and space — the canonical answer. The one-pass design is elegant and worth explaining: you check before you insert so you never use the same index twice.
Glean-specific tips
Glean interviews lean heavily on search and retrieval concepts. When explaining your solution, draw a parallel: 'This is basically an inverted index lookup — I'm trading space for O(1) query time, which is exactly what an enterprise search engine does.' That framing lands well with Glean engineers.
Common mistakes
- Returning values instead of indices — re-read the problem statement carefully.
- Checking seen.has(nums[i]) instead of seen.has(complement) — you want the partner, not yourself.
- Using an array-based lookup that assumes a bounded integer range — a Map is more general.
- Inserting before checking — if target is 2*nums[i], you'd incorrectly match an element with itself.
Follow-up questions
An interviewer at Glean may pivot to one of these next:
- Two Sum II (LC 167) — array is sorted; can you solve in O(1) space with two pointers?
- Three Sum (LC 15) — extend to three numbers summing to zero.
- What if there are multiple valid pairs? Return all of them.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Can I sort and use two pointers?
Yes, but sorting loses the original indices. You'd need to store (value, original_index) pairs before sorting, adding code complexity. The hash-map approach is cleaner for this exact problem.
Why insert after checking?
To avoid matching nums[i] with itself when target == 2*nums[i]. Checking first ensures the complement is a previously seen element at a different index.
Practice these live with InterviewChamp.AI
Drill Two Sum and other Glean interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →