Skip to main content

2. Two Sum

easyAsked at Notion

Given an array and a target, return the indices of two numbers that add to the target. Notion uses this as a warm-up to gauge how quickly you reach for a hashmap.

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

Source citations

Public interview reports confirming this problem appears in Notion loops.

  • Glassdoor (2026-Q1)Two Sum is Notion's most common phone screen warm-up.
  • LeetCode Discuss (2025)Reported as the 5-minute opener before the real question.

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

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 — never the answer Notion wants.

2. Hashmap single pass (optimal)

For each num, check if target - num was seen. Store num -> index as you go.

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: Single pass, O(n). The key insight: complement lookup is O(1) with a hashmap. Don't insert before checking — otherwise nums[i]+nums[i] could double-count.

Notion-specific tips

Notion sees Two Sum as a culture check more than a difficulty check — they want clean code, immediate hashmap recognition, and a 2-minute solution. Bonus: state complexity unprompted, mention that returning indices (not values) means you must store position.

Common mistakes

  • Inserting nums[i] into the map BEFORE checking — produces a false positive when nums[i]*2 == target.
  • Using an object instead of Map and tripping on number-vs-string keys for negative numbers.
  • Returning values instead of indices.

Follow-up questions

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

  • What if the array is sorted? (Two-pointer, O(1) space)
  • Return all pairs, not just one.
  • Three Sum (LC 15) generalizes this with sort + two-pointer.

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 index, not just presence?

The problem requires returning indices. A Set would only confirm presence; you need the position of the earlier number.

What if no solution exists?

The constraint says one solution exists, so return is guaranteed. In real code, handle the no-match path with null or an exception.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →