1. Two Sum
easyAsked at RedditGiven an array of integers and a target, return indices of the two numbers that add up to target. Reddit uses this as a warm-up to test if you reach for a hash map without prompting — the same reflex that powers vote-deduplication and karma-event lookups.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit phone-screen warmup reported across multiple eng tracks.
- LeetCode Discuss (2025-11)— Cited as the 'first 5 minutes' problem before harder questions.
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]Approaches
1. Nested loop brute force
Try every pair of indices.
- 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];
}
}
}Tradeoff: Quadratic. Acceptable only as a first-pass mention before jumping to the hash map.
2. Single-pass hash map (optimal)
While walking the array, record value->index. For each new value, check if (target - value) was already seen.
- Time
- O(n)
- Space
- O(n)
function twoSum(nums, target) {
const seen = new Map();
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);
}
}Tradeoff: Linear time with O(n) memory. Same pattern Reddit uses for vote-event lookup and karma aggregations.
Reddit-specific tips
Reddit interviewers want to see the hash-map reflex immediately. Bonus signal: mention that the same pattern powers their vote-deduplication (user_id -> last_vote) and reaching for a hash before brute force shows you'd reach for Redis before SQL JOINs at scale.
Common mistakes
- Returning values instead of indices.
- Adding the current number to the hash before checking complement — risks matching an element with itself.
- Not handling negative numbers (constraint allows -10^9).
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- Three Sum (LC 15) — same hash-map insight, one extra outer loop.
- What if the array is sorted? Switch to two-pointer for O(1) space.
- What if input streams in? Maintain a running hash.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why hash map instead of sorting?
Sorting loses the original indices. If we needed values not indices, two-pointer after sort works and uses O(1) extra space.
Can this overflow?
In JavaScript values up to 2^53 - 1 are safe. With constraints of 10^9, target - nums[i] cannot overflow.
Practice these live with InterviewChamp.AI
Drill Two Sum and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →