Skip to main content

1. Two Sum

easyAsked at Pinterest

Two Sum is Pinterest's warm-up screening question: given an integer array and a target, return the indices of two numbers that add up to the target. The interviewer is checking that you can articulate the space-for-time tradeoff before writing the optimal hash-map solution.

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

Source citations

Public interview reports confirming this problem appears in Pinterest loops.

  • Glassdoor (2026-Q1)Pinterest SWE-new-grad phone-screen reports list Two Sum as a 10-minute warm-up before a harder ranking-flavored follow-up.
  • LeetCode Pinterest tag (2026-Q1)Top-frequency tagged problem on the Pinterest company list.

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

Try every (i, j) pair with i < j and return when nums[i] + nums[j] == target.

Time
O(n^2)
Space
O(1)
function twoSumBrute(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: Zero extra space and simple to reason about. At n = 10^4 it still runs in under a second, but most Pinterest interviewers will expect you to name this as the wrong final answer before moving on.

2. Hash map (optimal, one-pass)

Store each value's index in a hash map as you scan. For each new num, look up target - num.

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: O(n) time at O(n) extra space — the canonical tradeoff. One-pass works because by the time you'd want to pair X with Y, X has already been inserted on a prior iteration.

Pinterest-specific tips

Pinterest interviewers run this as a 'are you a real coder' screening before pivoting to a more interesting recommendation-flavored question. Narrate the brute-force out loud, name the O(n^2) cost, then say 'I'll trade space for time' and write the hash-map version. Don't skip the narration — Pinterest's rubric weighs communication on the warm-up.

Common mistakes

  • Returning the values instead of the indices.
  • Writing two passes (build the map, then lookup) when one pass is cleaner.
  • Forgetting duplicates: nums = [3, 3] with target 6 must return [0, 1] — the one-pass version handles this correctly because the second 3 finds the first in the map before being inserted itself.

Follow-up questions

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

  • What if the array were sorted? (Two-pointer in O(1) extra space.)
  • What if you needed all pairs that sum to the target, not just one? (Frequency map with duplicate handling.)
  • Extend to Three Sum: sort + fix one + two-pointer the rest, O(n^2).

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Does Pinterest still ask Two Sum in 2026?

Yes, but only as a 10-minute warm-up on phone screens. The harder follow-up is typically the real signal — expect a pivot to Three Sum or a feed-ranking variant within 15 minutes.

Is the one-pass hash map version actually expected?

Both one-pass and two-pass pass the rubric on correctness, but one-pass is the canonical answer because it shows you understand the invariant: when you arrive at index i, every prior index is already in the map.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →