Skip to main content

1. Two Sum

easyAsked at Vercel

Given an array of integers and a target, return the indices of two numbers that add up to the target. Vercel asks this as a warm-up to test whether you reach for a hash map instinctively when you see a 'find a pair' problem in their edge-routing or cache-key dedup contexts.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)Vercel phone-screen warm-up; expected in under 10 minutes.
  • LeetCode Discuss (2026-Q1)Listed in Vercel front-end + platform engineer onsite recaps.

Problem

Given an array of integers nums and an integer target, return the 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^9
  • -10^9 <= target <= 10^9
  • Only one valid answer exists.

Examples

Example 1

Input
nums = [2,7,11,15], target = 9
Output
[0,1]

Explanation: nums[0] + nums[1] = 2 + 7 = 9.

Example 2

Input
nums = [3,2,4], target = 6
Output
[1,2]

Approaches

1. Brute force double loop

Try every pair (i, j) with i < j and check if they sum to 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];
    }
  }
}

Tradeoff: Quadratic. Acceptable only if you flag it as the warm-up and immediately offer the hash-map upgrade.

2. Hash map single pass (optimal)

Walk the array once. For each value v at index i, check if target - v is already in the map; if so 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);
  }
}

Tradeoff: Single pass, O(n) time. The key insight: store-then-check vs check-then-store ordering prevents the same-element-twice bug.

Vercel-specific tips

Vercel screens this as a 10-minute warm-up; the bar is not the algorithm — it's whether you write idiomatic JavaScript (Map over object literal for non-string keys), handle the same-index-twice edge case correctly, and narrate cleanly. Bonus signal: relating it to a real edge-cache scenario like 'this is how I'd dedup request-fingerprint hashes.'

Common mistakes

  • Using a plain object {} as the map — breaks for negative numbers or string keys that collide with prototype properties like '__proto__'.
  • Storing all values first, then doing a second pass — works but doubles passes; the optimal does it in one.
  • Returning [i, j] where i > j without checking — the problem says any order is fine but some interviewers grade for sorted output anyway; ask.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • What if the array is sorted? Two-pointer gives O(1) extra space (LC 167).
  • Return ALL pairs that sum to target (LC 1, variant).
  • Three Sum — extend to triples (LC 15).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why Map instead of a plain object?

Map preserves insertion order, accepts any key type (including numbers as numbers, not coerced to strings), and avoids prototype-pollution edge cases. Vercel interviewers notice when you reach for the right primitive.

What if there are multiple valid answers?

The problem guarantees exactly one. If asked the variant where many exist, switch to a set of seen values and collect all matching pairs.

Practice these live with InterviewChamp.AI

Drill Two Sum and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →