Skip to main content

1. Two Sum

easyAsked at DRW

DRW uses Two Sum as a rapid-fire calibration question — they want to see the hash-map instinct fire in under 30 seconds. At a firm where order-matching engines process millions of ticks per second, the difference between O(n) and O(n²) lookup is not theoretical.

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

Source citations

Public interview reports confirming this problem appears in DRW loops.

  • Glassdoor (2026-Q1)DRW SWE phone screen reports consistently mention Two Sum as an opening calibration question, graded heavily on immediate O(n) approach.
  • Blind (2025-10)Multiple DRW SWE candidate threads cite Two Sum as a warm-up that quickly pivots to latency and throughput follow-ups.

Problem

Given an array of integers nums and an integer target, return the indices of the two numbers that add up to target. You may assume 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
  • Exactly 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 (O(n²))

Try every pair. State this only to show you know it exists — never submit it as your answer at DRW.

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: O(n²) — unacceptable at DRW's data volumes. Mention it in one sentence, then pivot immediately to the hash-map approach.

2. Hash map (one pass)

For each element, check if its complement (target − nums[i]) exists in the map. If yes, return the pair. Otherwise, record the current element and its index.

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: Single pass, O(n) time, O(n) space. This is the expected answer at DRW. State the approach and complexity before writing code.

DRW-specific tips

DRW interviewers pivot quickly from correctness to throughput: 'We process 10 million price-update events per second. If n = 10,000 per batch, how many operations per second does O(n²) vs O(n) imply?' Be ready to compute: O(n²) = 10^8 ops per batch × 10^6 batches/sec = completely infeasible. Also note that on a sorted order book array you'd use two pointers in O(1) space — DRW likes knowing you can choose the right tool given data properties.

Common mistakes

  • Opening with the nested-loop solution without immediately flagging the O(n) alternative — DRW treats this as a signal that you don't think about performance by default.
  • Forgetting that the same index cannot be used twice — verify nums = [3, 3], target = 6 returns [0, 1].
  • Using indexOf() for complement lookup inside the loop — O(n) lookup turns the overall algorithm into O(n²) again.
  • Not handling negative numbers — the complement formula works for all integers, but explicitly confirm this.

Follow-up questions

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

  • Two Sum II (LC 167) — array is sorted; solve in O(1) space with two pointers.
  • Three Sum (LC 15) — extend to triplets; DRW frequently chains this in the same session.
  • What is the expected value of comparisons before finding a hit if the target pair is at a uniformly random position?

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 DRW ask an 'easy' problem like Two Sum?

It is a latency-instinct filter. The nested-loop reflex signals someone who does not think in terms of throughput. DRW's order-matching infrastructure runs at sub-millisecond latencies — O(n) vs O(n²) mindset matters from day one.

Two-pointer vs hash map — which does DRW prefer?

Hash map for the unsorted general case. If data is pre-sorted (as order-book price levels often are), two-pointer saves O(n) space and is preferred.

Should I sort and binary search?

Sorting is O(n log n) and loses original indices. Only prefer this over hash map if you must preserve O(1) extra space and can afford O(n log n) time.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →