Skip to main content

1. Two Sum

easyAsked at eBay

eBay phone screens frequently open with Two Sum as a warm-up to test hash-map fluency. In an e-commerce context, think of it as finding two item prices in a cart that sum to a target gift-card value — a dead-simple scenario that reveals whether you default to O(n²) brute force or reach for a hash map immediately.

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

Source citations

Public interview reports confirming this problem appears in eBay loops.

  • Glassdoor (2026-Q1)Reported as a common eBay phone-screen opener for new grad and junior SWE roles.
  • Blind (2025-10)Multiple eBay SWE threads confirm Two Sum as a standard first-round warm-up before harder problems.

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

Example 2

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

Explanation: nums[1] + nums[2] = 2 + 4 = 6.

Approaches

1. Brute Force

Check every pair of elements to find the two that sum to 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 reason about but O(n²) — too slow for large inventories. Mention this first, then immediately pivot to the hash-map solution.

2. Hash Map (One Pass)

Store each number's index in a map as you iterate. For each element, check if its complement (target − num) already exists in the map.

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 correct answer. The one-pass variant is preferred over two-pass because it handles duplicates correctly and is marginally more elegant.

eBay-specific tips

eBay interviewers use Two Sum to see if you communicate your thought process clearly. Walk through the hash-map insight out loud: 'For each element, I need to know if its complement exists — that's a lookup, so I use a map.' At eBay's scale (millions of listings), O(n²) is a non-starter; the interviewer expects you to flag this explicitly. Be ready for the follow-up: 'What if the array is sorted?' (two-pointer, O(1) space).

Common mistakes

  • Using the same index twice — always check that the complement index differs from the current index when using a two-pass approach.
  • Returning values instead of indices — the problem asks for indices.
  • Not handling negative numbers — the hash-map approach handles them transparently, but a sorting approach changes indices.
  • Over-engineering: spending time on edge cases that the constraints rule out (exactly one solution is guaranteed).

Follow-up questions

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

  • Two Sum II — array is sorted; use two pointers for O(1) space.
  • Three Sum — extend to three numbers summing to zero; requires sorting + two-pointer inner loop.
  • How would you handle multiple valid pairs if the constraint 'exactly one solution' were removed?

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Can I sort the array first?

Sorting would lose the original indices, making index-based answers incorrect unless you store them alongside the values. The hash-map approach is simpler and preserves indices.

Why one-pass over two-pass hash map?

One-pass stores the complement check and insertion in a single loop, is slightly more efficient in constant factors, and avoids the need to re-traverse the array.

What if nums has duplicate values?

The hash map stores the most recent index for a value. Since the problem guarantees exactly one solution, duplicates are safe — one of them will be the correct complement.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →