Array & Hashing
Array and hash table problems form the foundation of algorithmic problem-solving. This category focuses on efficient data access, frequency counting, and lookup operations using arrays and hash maps.Key Patterns & Techniques
1. Hash Map for O(1) Lookups
- Store elements for constant-time access
- Track seen elements or complements
- Map elements to indices or frequencies
2. Frequency Counting
- Use Counter or hash map to count occurrences
- Group elements by properties
- Find duplicates or unique elements
3. Prefix Sum
- Precompute cumulative sums for range queries
- Use with hash map for subarray problems
- Space-time tradeoff for multiple queries
4. Sorting + Hash Map
- Sort for grouping similar elements
- Use hash map for quick lookups after sorting
- Interval problems often require sorting first
Common Approaches
Two Sum Pattern
Frequency Counting Pattern
Prefix Sum Pattern
Problems in This Category
017. Two Sum
Easy | Frequency: 76.4%Classic hash map pattern - find two numbers that sum to target in O(n) time.
016. Merge Intervals
Medium | Frequency: 77.9%Sort by start time, then merge overlapping intervals in single pass.
024. K Closest Points to Origin
Medium | Frequency: 71.4%Find k closest points using heap or quickselect partitioning.
026. Subarray Sum Equals K
Medium | Frequency: 71.4%Prefix sum with hash map to find subarrays summing to target.
023. Custom Sort String
Medium | Frequency: 73.2%Count frequencies and rebuild string according to custom order.
022. Buildings With an Ocean View
Medium | Frequency: 74.9%Track maximum height from right to find buildings with ocean view.
062. Group Shifted Strings
Medium | Frequency: 47.0%Group strings by their shift pattern using hash map.
059. Continuous Subarray Sum
Medium | Frequency: 47.0%Prefix sum modulo with hash map for subarray divisibility.
077. Car Pooling
Medium | Frequency: 40.7%Interval scheduling with capacity constraints.
089. Range Sum Query - Immutable
Easy | Frequency: 32.0%Precompute prefix sums for O(1) range queries.
087. Maximum Subarray
Medium | Frequency: 32.0%Kadane’s algorithm for maximum contiguous subarray sum.
071. Managers with at Least 5 Direct Reports
Medium | Frequency: 40.7%SQL/hash map problem for counting grouped relationships.
Complexity Characteristics
| Pattern | Time Complexity | Space Complexity | When to Use |
|---|---|---|---|
| Hash Map Lookup | O(n) | O(n) | Need O(1) access, two sum variants |
| Frequency Count | O(n) | O(k) | Counting, grouping, anagrams |
| Prefix Sum | O(n) | O(n) | Range queries, subarray sums |
| Sorting + Hash | O(n log n) | O(n) | Intervals, need sorted order |
| Sliding Window + Hash | O(n) | O(k) | Substring/subarray with constraints |
Interview Tips
Pattern Recognition
- “Find two elements that…” → Hash map for O(n) solution
- “Count frequency/occurrences” → Counter or hash map
- “Subarray sum equals k” → Prefix sum with hash map
- “Group by property” → Hash map with computed keys
- “Merge overlapping…” → Sort first, then single pass
Common Mistakes
- Forgetting to handle duplicates in hash map
- Not initializing prefix sum hash with
{0: 1} - Modifying array while iterating over it
- Using list instead of hash set for O(1) lookups
- Not sorting intervals before merging
Optimization Strategies
- Use hash set instead of hash map when only checking existence
- Counter from collections is cleaner than manual dict updates
- Prefix sum trades space for time in range queries
- Sort first when dealing with intervals or grouping
- Single pass is often possible with the right hash map structure
Advanced Patterns
Rolling Hash
- For substring matching problems
- Rabin-Karp algorithm
- Time: O(n), Space: O(1)
Multi-level Hashing
- Nested hash maps for 2D problems
- Group by multiple properties
- Time: O(n), Space: O(n)
Index Mapping
- Map values to indices for O(1) updates
- Used in problems like “First Missing Positive”
- In-place modifications using array indices as hash
Pro Tip: When you see “in O(n) time”, think hash map. When you see “subarray sum”, think prefix sum with hash map. When you see “intervals”, think sort first.