Skip to main content

1. Two Sum

easyAsked at LinkedIn

Two Sum is LinkedIn's canonical 10-minute warm-up: given an integer array and a target, return the indices of the two numbers that add up to target. The interviewer is grading your willingness to narrate brute-force first, then move to the hash-map optimization.

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

Source citations

Public interview reports confirming this problem appears in LinkedIn loops.

  • Glassdoor (2026-Q1)LinkedIn SWE phone-screen reports list Two Sum as a 10-minute warmup, often paired with a follow-up like Two Sum II.
  • Blind (2025-12)LinkedIn new-grad writeups consistently mention Two Sum as the warmup before a harder algo problem on the same screen.

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
  • -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] == 9, so we return [0, 1].

Example 2

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

Example 3

Input
nums = [3,3], target = 6
Output
[0,1]

Approaches

1. Brute-force nested loops

Try every (i, j) pair with i < j and return when nums[i] + nums[j] == target.

Time
O(n^2)
Space
O(1)
function twoSumBrute(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: Easiest to write but n^2 at n = 10^4 is 10^8 ops, borderline TLE in interview environments. Always name this version first to set up the optimization.

2. Hash map one-pass (optimal)

Store each value's index in a hash map as you scan. For each new num, look up target - num.

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);
  }
  return [];
}

Tradeoff: O(n) time at O(n) extra space. The one-pass works because by the time you'd want to pair X with Y, you've already inserted whichever appeared first.

LinkedIn-specific tips

LinkedIn interviewers consistently want the brute-force → optimal transition narrated. Say: 'I can do this in n^2 with nested loops; let me trade space for time using a hash map.' If you jump straight to the optimal, the interviewer often asks 'what's the simpler version?' to probe your communication. LinkedIn loops also frequently follow up with Two Sum II (sorted-input, two-pointer) — be ready.

Common mistakes

  • Returning the values instead of the indices.
  • Using a hash map but writing it as two passes (build then lookup) when one-pass is cleaner.
  • Forgetting that duplicate values are allowed (nums = [3,3] with target 6 must return [0,1]).

Follow-up questions

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

  • Two Sum II - Input Array Is Sorted (LC 167) — two-pointer in O(1) extra space.
  • Three Sum (LC 15) — sort + fix one + two-pointer the rest.
  • Two Sum III - Data Structure Design (LC 170) — add/find API instead of static query.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Does LinkedIn really still ask Two Sum?

Yes — as a 10-minute warm-up to confirm you can code at all before the harder follow-up. Expect the interviewer to pivot to Two Sum II (sorted input) or one of LinkedIn's signature questions within 15 minutes.

Is the one-pass hash map actually expected, or is two-pass acceptable?

Both pass on correctness, but one-pass is the canonical answer. It demonstrates you understand the invariant: when you arrive at index i, the map contains every prior index, so any pair whose later half is i has its earlier half stored.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →