1. Two Sum
easyAsked at AndurilFind two indices in an array whose values sum to a target. Anduril uses this as a warm-up to gauge hash-map fluency and whether you can reason about the time-space tradeoff before reaching for the brute-force double loop.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Anduril loops.
- Glassdoor (2026-Q1)— Cited in Anduril SWE phone-screen reports as an opening warm-up question.
- Blind (2025-10)— Anduril threads confirm Two Sum or a variant appears in early-round screens to test baseline data-structure knowledge.
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^9Only one valid answer exists.
Examples
Example 1
nums = [2,7,11,15], target = 9[0,1]Explanation: nums[0] + nums[1] = 2 + 7 = 9.
Example 2
nums = [3,2,4], target = 6[1,2]Explanation: nums[1] + nums[2] = 2 + 4 = 6.
Approaches
1. Brute Force
Try every pair with two nested loops. Simple but O(n^2) time.
- 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];
}
}
return [];
}Tradeoff: Fine for tiny inputs but fails at scale. Anduril will push back immediately — mention this is O(n^2) and pivot to the hash-map approach.
2. Hash Map (one pass)
For each element, check if its complement (target - nums[i]) is already in the map. Store index as value.
- 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: Single pass, O(n) time and space. The complement check must come before inserting nums[i] to avoid using the same element twice.
Anduril-specific tips
Anduril interviewers care about deterministic correctness and clean reasoning. State your invariants aloud: 'I store value-to-index so I can look up the complement in O(1). I check before inserting to handle cases where target = 2 * nums[i].' Embedded and systems engineers at Anduril value explicit reasoning over clever one-liners. Be ready to analyze memory overhead — they may ask how you'd handle a 10^8-element stream.
Common mistakes
- Inserting nums[i] into the map before checking for its complement — this allows using the same index twice when nums[i] == target / 2.
- Returning values instead of indices — re-read the problem: the output is index pairs.
- Not handling negative numbers — the hash-map approach handles them transparently, but double-loop solutions sometimes have off-by-one issues.
- Sorting the array first — sorting destroys original indices, making the return values incorrect without an index-preservation step.
Follow-up questions
An interviewer at Anduril may pivot to one of these next:
- What if the array is sorted? Can you solve it with O(1) extra space using two pointers?
- What if there can be multiple valid answers? Return all of them.
- How would you handle a streaming input where you can't store the whole array?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why check for the complement before inserting?
If you insert first, then when target equals 2 * nums[i], seen.has(complement) would find the same element you just inserted, which is invalid.
Does the order of the returned indices matter?
The problem says 'any order', so [1,0] and [0,1] are both correct.
What if nums has duplicates?
The hash map stores the most recent index for a given value. Since the problem guarantees exactly one solution, duplicates that are part of the answer will still be found correctly on the second encounter.
Practice these live with InterviewChamp.AI
Drill Two Sum and other Anduril interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →