Skip to main content

1. Two Sum

easyAsked at Asana

Given an array of integers and a target, return the indices of two numbers that add up to the target. Asana opens with this to confirm you reach for hash maps reflexively before quadratic loops — a baseline signal before they pivot to graph problems.

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

Source citations

Public interview reports confirming this problem appears in Asana loops.

  • Glassdoor (2026-Q1)Phone-screen warmup reported by Asana early-career candidates.
  • LeetCode Discuss (2025-12)Cited as the first 10-minute filter at Asana before graph 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. 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.

Example 2

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

Approaches

1. Brute force nested loops

Check 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 — fine for tiny inputs but Asana expects you to skip past this in 30 seconds.

2. Hash map single pass

Store complement -> index as you scan. When you see the complement already, return both indices.

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 version is preferred over scan-then-search because it halves the constant factor.

Asana-specific tips

Asana uses this as a vibe check — they want to see you reach for a hash map without prompting and articulate why you map value to index (not the other way). If you draw the lookup table on the whiteboard before coding, that's bonus signal that you think about data structures before syntax.

Common mistakes

  • Returning the values instead of the indices.
  • Allowing the same element twice (e.g., target=6, nums=[3], returning [0,0]).
  • Storing the value as the key but then trying to look up by index later.

Follow-up questions

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

  • What if the array is sorted? (Two-pointer, O(1) space.)
  • What if you need ALL pairs that sum to target, not just one?
  • Extend to Three Sum — how does the complexity shift?

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 store value->index instead of index->value?

Because you're looking up by 'have I seen the complement value' — value is the search key. Index->value would force a linear scan of values to find the match.

Do duplicates in nums cause problems?

No — you write each value to the map, and if a later duplicate completes a pair, the lookup hits and you return immediately before overwriting.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →