Skip to main content

1. Two Sum

easyAsked at Elastic

Find two indices in an array whose values sum to a target. At Elastic, this tests your instinct for trading memory for speed — the same hash-map lookup pattern powers fast term lookups inside Elasticsearch's inverted index.

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

Source citations

Public interview reports confirming this problem appears in Elastic loops.

  • Glassdoor (2026-Q1)Reported as a warm-up phone-screen question in multiple Elastic SWE interview threads.
  • Blind (2025-10)Elastic candidates note Two Sum appears in early rounds to verify hash-map fluency before harder 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 has exactly one solution, and you may not use the same element twice. 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.

Example 2

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

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

Approaches

1. Brute force — nested loop

For every pair (i, j) check if nums[i] + nums[j] === target. O(n²) time, O(1) space. Useful only to establish the baseline.

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: Simple to reason about, but quadratic — not acceptable for large datasets Elastic ingests at scale.

2. Hash map — single pass

Walk the array once. For each element, check whether (target - nums[i]) already exists in a hash map. If yes, return both indices. If no, store the current value → index mapping.

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: O(n) time and O(n) space. The canonical answer Elastic expects. Mirrors the constant-time term lookup concept in inverted-index data structures.

Elastic-specific tips

Elastic interviewers often pivot quickly after the hash-map solution to ask: 'What if the array were sorted — could you do it in O(1) space?' Be ready to describe the two-pointer approach (left/right converging). Also be prepared to discuss how this pattern generalizes to k-sum problems and why the hash-map approach hits O(n) for the sorted variant as well when negatives are present.

Common mistakes

  • Returning values instead of indices — read the problem carefully.
  • Using the same element twice — using seen.has before seen.set prevents this in the single-pass approach.
  • Forgetting negative numbers — complement = target - nums[i] works correctly for negatives, but brute-force index tricks often skip them.
  • Assuming sorted input — the hash-map solution works on unsorted arrays; two-pointer requires a sort step that changes indices.

Follow-up questions

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

  • 3Sum (LC 15) — find all unique triplets that sum to zero; requires sorting + two pointers.
  • What if multiple solutions exist — how would you return all pairs?
  • How would you adapt this if the array is a stream you cannot store entirely in memory?

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 does inserting after checking prevent using the same element twice?

Because you only look up the complement in elements seen before index i. If nums[i] itself were the complement, it would not yet be in the map at the time of the lookup.

Does Map.get() count as O(1)?

Yes — JavaScript Map uses a hash table internally, giving expected O(1) get and set, matching Java HashMap semantics that Elastic's Java stack relies on.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →