Skip to main content

1. Two Sum

easyAsked at Palantir

Given an array of integers and a target, return indices of the two numbers that sum to target. Palantir asks this early as a warm-up to gauge whether you reach for hash-map lookups immediately or default to nested loops, since their ETL pipelines join entities by key constantly.

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

Source citations

Public interview reports confirming this problem appears in Palantir loops.

  • Glassdoor (2026-Q1)FDE phone-screen warm-up reported across multiple Palantir Foundry interviews.
  • Blind (2025-11)Cited as common opener before Palantir asks an entity-resolution follow-up.

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. 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]

Approaches

1. Brute force double loop

Check every pair (i, j) where i < j.

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; fine for n=20 but blows up at scale. Palantir interviewers will push you to do better.

2. Hash map single pass

Walk the array once; for each value, look up (target - value) in a hash. If found, return the pair.

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, linear space. The single-pass insight (instead of building the map first) is the bonus signal.

Palantir-specific tips

Palantir grades early-screen problems on whether you articulate the time/space tradeoff before coding. Mention that the hash-map pattern is the same one you'd use to join two tables on a key in a Foundry transform. If you blurt out 'O(n) with extra space' before the interviewer prompts, that's a strong signal.

Common mistakes

  • Building the full map first, then iterating again — works but wastes the single-pass elegance.
  • Forgetting the 'not using same element twice' clause and returning [i, i] when nums[i] = target/2.
  • Using indexOf inside the loop — that's still O(n^2).

Follow-up questions

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

  • Return all pairs that sum to target, not just one.
  • What if the array is sorted? (Two-pointer becomes O(n) with O(1) space.)
  • Extend to three-sum: find a, b, c such that a + b + c = target.

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 is the hash-map approach considered the canonical answer?

It's O(n) time with O(n) space — optimal for unsorted input. The single-pass version also handles streaming data naturally, which matches how Palantir pipelines join records as they arrive.

What if there are duplicates in the array?

The hash-map version still works: when you see the second occurrence, the first one is already stored. You return [firstIndex, currentIndex].

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →