Skip to main content

1. Two Sum

easyAsked at PayPal

Given an array of integers and a target, return indices of the two numbers that add up to the target. PayPal asks this to gauge whether you reach for a hash map immediately — the same reflex they want when you're matching a payment to a pending invoice by amount.

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

Source citations

Public interview reports confirming this problem appears in PayPal loops.

  • Glassdoor (2026-Q1)PayPal phone screen warm-up — interviewer expects O(n) hash-map solution within 10 minutes.
  • LeetCode Discuss (2025-11)Reported as PayPal SDE-1 first-round screening question.

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, so we return [0, 1].

Example 2

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

Example 3

Input
nums = [3,3], target = 6
Output
[0,1]

Approaches

1. Brute force nested loops

Check every pair (i, j) for nums[i] + nums[j] == 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 time — fails at PayPal's transaction-matching scale where n is in the millions.

2. Hash map single pass (optimal)

Iterate once. For each num, check if (target - num) is already in the map. If yes, return both indices. If no, store num.

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: Single pass, O(n). The pattern (store-then-lookup hash map) is the same one PayPal uses for invoice-to-payment reconciliation.

PayPal-specific tips

PayPal interviewers want to see that you don't reach for sorting (O(n log n)) when a hash map gives O(n). Mention that financial-precision matters: if these were dollar amounts in cents, you'd want integers not floats. Bonus signal: ask whether duplicates count as the same element — that maps directly to PayPal's idempotency-key semantics.

Common mistakes

  • Using the same index twice — must check that seen.get(complement) !== i.
  • Returning the values instead of indices (the problem asks for indices).
  • Using floats for cents — at PayPal scale, this introduces rounding bugs.

Follow-up questions

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

  • What if the array is sorted? (Use two-pointer for O(1) space.)
  • What if you need ALL pairs that sum to target, not just one?
  • What if the array is a stream you can only iterate once?

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 not sort first?

Sorting is O(n log n) and loses the original indices. Hash map is O(n) and preserves index information natively. At PayPal's data volumes, the log n factor matters.

What about hash collisions?

JavaScript's Map uses well-distributed hashing so average lookup stays O(1). For interview purposes, you treat hash-map ops as O(1).

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →