Skip to main content

1. Two Sum

easyAsked at Hugging Face

Find two numbers in an array that add up to a target. Hugging Face uses this as a warm-up to test whether candidates think in hash maps — the same O(1) lookup mindset that underlies efficient tokenizer vocabulary lookups in ML pipelines.

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

Source citations

Public interview reports confirming this problem appears in Hugging Face loops.

  • Glassdoor (2026-Q1)Reported as a standard first-round screening problem in Hugging Face SWE phone interviews.
  • Blind (2025-10)Multiple Hugging Face interview threads list Two Sum as a common warm-up before harder system design questions.

Problem

Given an array of integers nums and an integer target, return the 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] = 2 + 7 = 9, so return [0, 1].

Example 2

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

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

Approaches

1. Brute Force

Check every pair of indices. Simple but O(n²).

Time
O(n²)
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: Easy to reason about, but not acceptable for large inputs. Mention this only as a baseline before pivoting to the hash map solution.

2. Hash Map (single pass)

Store each number's index in a map. For each element, check whether its complement (target - nums[i]) already exists in the map.

Time
O(n)
Space
O(n)
function twoSum(nums, target) {
  const seen = new Map();
  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: O(n) time and space. This is the expected answer. The single-pass variant processes each element once, checking and inserting in the same loop.

Hugging Face-specific tips

Hugging Face interviewers value clear articulation of the trade-off between time and space. Explain why you're trading O(n) space for O(n) time before writing the code. Draw a parallel to how vocabulary lookup tables in tokenizers use hash maps to achieve O(1) token-to-id resolution at inference time — this kind of ML context scores points.

Common mistakes

  • Using the same element twice — make sure to add to the map after checking, not before.
  • Returning values instead of indices — re-read the problem statement carefully.
  • Jumping to two-pointer before realizing that requires sorting (which destroys original indices).
  • Forgetting to handle negative numbers — the complement can be negative.

Follow-up questions

An interviewer at Hugging Face may pivot to one of these next:

  • Two Sum II — the array is already sorted; use two pointers instead of a hash map.
  • Three Sum — extend to triplets; hash map approach needs careful de-duplication.
  • How would you handle this if the input stream arrived element by element (online algorithm)?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Can I sort and use two pointers?

Yes, but sorting destroys the original indices. You'd need to store (value, originalIndex) pairs, making it more complex than the hash map solution without a better asymptotic profile.

Why a Map and not a plain object?

Map handles non-string keys correctly and has guaranteed O(1) lookup. A plain object works for integer keys but is semantically less clear.

Does order of insertion into the map matter?

Yes — insert after the complement check to avoid using the same element twice (e.g., target=6, nums=[3,...]: don't match 3 with itself).

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →