Skip to main content

1. Two Sum

easyAsked at Coinbase

Given an array of integers and a target, return the indices of two numbers that add up to the target. Coinbase asks this as a warm-up to gauge whether you reach for hash maps over nested loops on the very first problem.

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

Source citations

Public interview reports confirming this problem appears in Coinbase loops.

  • Glassdoor (2026-Q1)Coinbase phone-screen warm-up across multiple backend tracks.
  • Blind (2025-11)Coinbase L4 phone screen first-pass filter.

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^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] == 9.

Example 2

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

Approaches

1. Brute force nested loops

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 when n is tiny. Mention it once, move on.

2. Hash map one-pass

Walk the array once and look up target - current in a hash map of values seen so far. If present, you have the pair; otherwise store current 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: Single pass with O(n) extra memory. The key insight is asking 'what value would complete me?' rather than 'who can I pair with?'

Coinbase-specific tips

Coinbase grades this on speed and cleanliness — they want to see the hash-map pattern within 60 seconds of reading the prompt. Mentioning numerical-precision concerns (what if values were BigInt-sized amounts?) earns bonus signal because it matches their crypto domain. Avoid floats in any follow-up about prices.

Common mistakes

  • Using the same element twice — guard with i < j or by storing the index only AFTER the lookup.
  • Storing the value and overwriting on duplicates without thinking — duplicates are valid (e.g., [3,3], target=6).
  • Returning the values instead of the indices — re-read the prompt.

Follow-up questions

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

  • What if the array is sorted? (Use two pointers, O(1) space.)
  • Return all unique pairs instead of one.
  • What if values can overflow JS safe-integer range — would you use BigInt?

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 a Map instead of a plain object?

Map handles non-string keys natively and avoids prototype-pollution surprises. For numeric keys the perf difference is negligible, but Map signals intent.

Why one pass instead of two?

One pass is strictly fewer operations and avoids the edge case of finding the same element twice in a value-keyed map built from a separate first pass.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →