DSA Master › Topic 1
Arrays & Hashing
Most interview problems start here. A hash map turns "have I seen this before?" from an O(n) scan into an O(1) lookup, which is how you get most O(n²) brute-force solutions down to O(n).
Concepts
- HashMap
- HashSet
- Frequency counting
- Prefix sums
- Array indexing tricks
How to recognise it
- You need to find pairs, duplicates or complements (e.g. target − x).
- You need to count how often things appear, or group items by a key.
- Values are in the range 1..n, so the array itself can act as a hash table.
Common pitfalls
- Check the map before you insert the current element, or you'll pair an element with itself.
- To group by a key, use a canonical form (e.g. sorted chars or a 26-count signature).
Problems
- Two Sum (Easy)
- Contains Duplicate (Easy)
- Valid Anagram (Easy)
- Group Anagrams (Medium)
- Top K Frequent Elements (Medium)
- Product of Array Except Self (Medium)
- Longest Consecutive Sequence (Medium)
- Majority Element (Easy)
- Subarray Sum Equals K (Medium)
- First Missing Positive (Hard)
- Insert Delete GetRandom O(1) (Medium)
- Longest Harmonious Subsequence (Easy)