Skip to main content

1. Two Sum

easyAsked at Akamai

Find two indices in an array whose values sum to a target. Akamai asks this as a warm-up to probe hash map reasoning and O(n) thinking — the kind of constant-time lookup that matters when processing billions of log entries per day at the edge.

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

Source citations

Public interview reports confirming this problem appears in Akamai loops.

  • Glassdoor (2026-Q1)Reported as a phone-screen warm-up question in Akamai SWE interviews.
  • Blind (2025-10)Akamai interview threads confirm Two Sum appears in early-round coding screens.

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 exactly one solution exists, 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

Check every pair with a nested loop.

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];
    }
  }
}

Tradeoff: Simple but O(n²) — mention this only to frame the improvement. Akamai explicitly asks about scale implications; at 10^9 entries this is unusable.

2. Hash map (one pass)

As you iterate, store each value's index in a map. For each element, check whether its complement (target − nums[i]) is already in the map.

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);
  }
}

Tradeoff: Single pass, O(n) time and space. The complement check is the key insight. Akamai interviewers appreciate naming the O(1) average-case hash lookup explicitly.

Akamai-specific tips

Akamai screens for scale awareness. After presenting the O(n) solution, volunteer: 'At 10^9 requests/day the O(n²) approach would be roughly 10^18 operations — completely infeasible. The hash map gives us linear time at any data volume.' This signal of cost-at-scale thinking is exactly what Akamai looks for across all rounds.

Common mistakes

  • Returning values instead of indices — reread: the problem asks for indices.
  • Using the same element twice — the map check must happen before the current element is inserted.
  • Assuming a sorted array and jumping to two-pointer — that solves Two Sum II, not this variant.

Follow-up questions

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

  • Three Sum (LC 15) — extend to three elements summing to zero.
  • What if multiple valid pairs exist and you must return all of them?
  • How does your approach change if the input array is too large to fit 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 store the complement before the current element?

You insert nums[i] after the complement check to avoid using the same index twice. If complement === nums[i], inserting first would find a false self-match.

Is a two-pointer approach valid here?

Only if the array is sorted. Sorting first would cost O(n log n) and also destroy the original indices you need to return.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →