← JavaScript Coding

Two Sum

easyblind-75dsaarrayshashing
Asked at
Amazon
Google
LinkedIn
Meta
Microsoft
Amazon, Google, LinkedIn, Meta, Microsoft

Problem statement

Implement twoSum for the classic Two Sum problem.

This Blind 75 version is framed for frontend interviews: write clean JavaScript, name the invariant out loud, and handle edge cases without over-engineering.

What this tests

  • Pattern: hash map complement lookup.
  • Ability to explain time and space complexity clearly.
  • Careful handling of empty inputs, duplicates, and boundary cases.
  • Production-style JavaScript readability, not just competitive-programming shortcuts.

Requirements & constraints

  • →Use JavaScript without external libraries.
  • →Prefer the standard optimal interview approach for this pattern.
  • →Return deterministic output when multiple valid outputs exist where the prompt implies ordering.

Examples

  • Example 1

    Input: Use the canonical Blind 75 sample for this problem.

    Output: Return the value required by the problem statement.

How to approach Two Sum

The strategy an interviewer expects you to reach for.

You need two numbers that add to a target — the classic warm-up that still fails people who only nest loops. Naive move Brute force every pair in O(n²). Where it breaks Large arrays timeout; also easy to reuse the same index twice if you are careless. The fix One pass with a Map from value → index. For each x, look up target−x. Store after the check so you never reuse the same element. Dry-run: Example: nums=[2,7,11,15], target=9 → see 2, store {2:0}; at 7 find 2 in map → [0,1]. Remember: Hash the complement — do not scan the rest of the array for each x. Say this in the interview: I'd keep a map of seen values to indices and for each number check whether target minus that number is already in the map.

Premium

The full solution is part of HelloFrontend Pro

The question above is free to read in full. Upgrade to unlock the interactive workspace and the senior-level walkthrough that go with it.

  • Runnable editor with the hidden test suite
  • Progressive hints that unlock as you get stuck
  • Senior-level reference solution with a line-by-line walkthrough
Unlock the full solution →

Already a member? Log in to open the workspace.

More JS Coding questions

View all JS Coding →
JS CodingMedium

Data Merging (Gym Sessions)

JS CodingHard

Data Selection

JS CodingMedium

LRU Cache

JS CodingEasy

Contains Duplicate

JS CodingEasy

Valid Anagram

JS CodingMedium

Group Anagrams

Explore related prep hubs

  • All JavaScript coding
  • JavaScript interview questions
  • JS polyfill interview questions
  • Debounce & throttle questions
  • React interview hub
  • Amazon interview
  • Google interview
  • LinkedIn interview
  • Meta interview
  • Microsoft interview