MEDIUM NC#3 Blind #1 Arrays & Hashing

1. Two Sum

šŸ“– 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.

🧠 Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • •Array methods (`push`, `pop`, `shift`, `slice`)
  • •Object/Map/Set usage patterns
  • •Function parameter and return typing
  • •In-place array updates
  • •Sorted array traversal
  • •Boundary condition checks

Logical Thinking Concepts

  • •Define invariants before coding
  • •Check edge cases first (`[]`, single element, duplicates)
  • •Estimate time/space before implementation
  • •Apply Two Pointers reasoning pattern

šŸ’” Approach

  • → Use hash map to store seen numbers and their indices
  • → For each number, check if complement (target - num) exists in map
  • → If found, return indices
  • → Time: O(n), Space: O(n)

šŸ› ļø Hints & Pitfalls

Hints

  • •Use hash map to store seen numbers and their indices
  • •For each number, check if complement (target - num) exists in map
  • •If found, return indices

Common Pitfalls

  • •Time: O(n), Space: O(n)

🧪 Test Cases

Hidden tests on submit: 3

Test Case 1
Not run
Input:
twoSum([2, 7, 11, 15], 9);
Expected:
[0, 1]
Test Case 2
Not run
Input:
twoSum([-1, -2, -3, -4, -5], -8);
Expected:
[2, 4]
Test Case 3
Not run
Input:
twoSum([0, 4, 3, 0], 0);
Expected:
[0, 3]

šŸ“ Code Editor

šŸ“š Reference Solution

ā–¼
⌘K Search āŒ˜ā†© Run ⌘S Submit