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)
š§ Prerequisites
š ļø 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
š¤ Output