Skip to main content

1. Two Sum

easyAsked at Plaid

Given an array of integers and a target, return indices of two numbers that sum to the target. Plaid asks this as a warm-up before harder ETL problems to verify you reach for a hash map rather than the nested-loop instinct.

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

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid phone screen warm-up across SWE I/II interviews.
  • LeetCode Discuss (2026-Q1)Reported as Plaid OA opener.

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]

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 time. Acceptable only as a starting point you immediately optimize.

2. Hash map single pass

Walk the array once. For each number, check if (target - num) is already in the map.

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: Linear time with O(n) extra space. Store as you go — don't pre-populate then re-scan, that allows accidental reuse of the same index.

Plaid-specific tips

Plaid uses this as a temperature check before financial-data problems. Articulate why the hash map approach lets you avoid the duplicate-index bug, and mention how the same pattern shows up later when reconciling two ledgers. Bonus signal: ask about duplicates and negative numbers before coding.

Common mistakes

  • Pre-filling the hash map then iterating — risks matching an index with itself.
  • Using nums.indexOf inside the loop — hidden O(n) lookup degrades to O(n^2).
  • Returning values instead of indices — re-read the prompt.

Follow-up questions

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

  • What if the input is sorted? (two pointers, O(1) extra space)
  • Return all pairs that sum to target without duplicates.
  • Stream version: numbers arrive one by one — same API.

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 negative-number keys and very large integer keys without coercion bugs. For a Plaid problem where the inputs are transaction amounts in cents, Map is the safer default.

What's the duplicate-index trap?

If nums = [3,3] and target = 6, you must return [0,1] not [0,0]. Inserting after the lookup (not before) is what prevents this.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →