On this page
Two Sum
Solving Two Sum using Hash map.
Problem
Find two numbers that add to target.
Pattern
This problem demonstrates the Hash map pattern.
Approach
Use HashMap to store complements.
Solution
// Solution for Two Sum
// Pattern: Hash map
// O(n) time, O(n) space
Complexity
O(n) time, O(n) space
Best Practices
- Identify the pattern before coding — pattern recognition saves time
- Handle edge cases: empty input, single element, duplicates
- Use descriptive variable names even in timed interviews
- Test with the provided examples plus one custom case