Skip to main content

1. Two Sum

easyAsked at Linear

Find two numbers in an array that add to a target. Linear asks this as a warm-up to see if you reach for the O(n) hash-map solution immediately or stumble through O(n²) brute force first.

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

Source citations

Public interview reports confirming this problem appears in Linear loops.

  • Glassdoor (2026-Q1)Recurring warm-up in Linear SWE phone screens per recent reviews.
  • Blind (2025-11)Cited in multiple Linear interview threads as a Round 1 starter.

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.

Constraints

  • 2 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 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]

Explanation: nums[1] + nums[2] = 6.

Approaches

1. Brute force pairs

Check every pair with two nested loops.

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: Easy to write, but Linear graders expect you to NOT stop here. Use this to open the conversation, then optimize.

2. Hash map (optimal)

Single pass; for each element compute the complement and look it up in constant time.

Time
O(n)
Space
O(n)
function twoSum(nums, target) {
  const seen = new Map();
  for (let i = 0; i < nums.length; i++) {
    const comp = target - nums[i];
    if (seen.has(comp)) return [seen.get(comp), i];
    seen.set(nums[i], i);
  }
}

Tradeoff: One pass, O(n) time. The mental model: every problem that turns O(n) lookup into O(1) is suspicious of a hash map.

Linear-specific tips

Linear's SWE interviews lean medium-difficulty algorithms with strong articulation expectations. Walk through brute-force first, then explicitly say 'we can do better' and pivot. Don't skip the brute force entirely — interviewers want to see the optimization step, not just the optimal answer.

Common mistakes

  • Skipping the brute-force baseline — the interviewer can't grade your optimization without seeing it.
  • Returning the values instead of indices.
  • Not considering the edge case where nums.length is exactly 2.
  • Using the same element twice (e.g., target=6, nums=[3,3] is fine, but nums=[3,x] with one 3 is not).

Follow-up questions

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

  • Two Sum II (sorted input) — use two pointers in O(1) space instead.
  • 3Sum — anchor + two-pointer pattern.
  • What if there are multiple valid answer pairs?

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 use a hash map?

Constant-time lookup turns the inner O(n) scan into O(1), giving O(n) total — a classic time-for-space trade.

Why store the index, not the value?

The problem asks for indices. The value is already implicit from nums[i], so storing the index is sufficient.

What if two elements have the same value?

The map stores the most recently seen index, but you check for the complement before overwriting, so duplicate values are handled correctly.

Can I sort first?

Sorting loses the original indices. You'd need an extra array to track them, which is messier than a direct hash map.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →