Practice SetLesson 061 min read

Hash Maps & Frequency — Practice Problems

November 05, 2026

// How to use this set

Work these in order: easy (Two Sum, Valid Anagram, Contains Duplicate, Ransom Note, First Unique Character, Majority Element, Isomorphic Strings) → medium (Group Anagrams, Top K Frequent Elements, Longest Consecutive Sequence, Subarray Sum Equals K, Valid Sudoku, LRU Cache). Cover the solution, solve it on paper, then type it out and run it. Every solution below was run and tested.

Thirteen problems that drill the hash map & frequency pattern from Lesson 6. The reps are the point — read the approach, try it yourself first, then check the code. Each solution here was actually run and tested.

How to practise. The hash-map move is almost always “trade memory for time”: store what you’ve seen so a future lookup is O(1). For each problem, name what you put in the map (value→index? char→count? prefix-sum→frequency?) before writing a line. That one decision is the whole solution.

The 13 problems

  1. 1Two Sum (LeetCode 1)EasyFind two indices whose values add to a target: remember each value's index and look up its complement.arrow_forward
  2. 2Valid Anagram (LeetCode 242)EasyCheck whether two strings use exactly the same letters: compare their character counts.arrow_forward
  3. 3Contains Duplicate (LeetCode 217)EasyReport whether any value appears twice: a set drops repeats, so compare its size with the list's.arrow_forward
  4. 4Ransom Note (LeetCode 383)EasyCan the note be cut from the magazine? Subtract letter counts and check nothing is left short.arrow_forward
  5. 5First Unique Character in a String (LeetCode 387)EasyFind the index of the first character that appears once: count everything, then scan in order.arrow_forward
  6. 6Majority Element (LeetCode 169)EasyFind the value that fills more than half the array: count it with a map, or cancel votes in O(1) space.arrow_forward
  7. 7Isomorphic Strings (LeetCode 205)EasyCheck if one string's letters can be renamed into the other's: keep maps in both directions.arrow_forward
  8. 8Group Anagrams (LeetCode 49)MediumBucket words that are anagrams of each other: use each word's sorted letters as a dictionary key.arrow_forward
  9. 9Top K Frequent Elements (LeetCode 347)MediumReturn the k most common values: count with a hash map, then pick the k largest counts.arrow_forward
  10. 10Longest Consecutive Sequence (LeetCode 128)MediumFind the longest run of consecutive integers in O(n): put them in a set and only count from run starts.arrow_forward
  11. 11Subarray Sum Equals K (LeetCode 560)MediumCount subarrays summing to k: each is a difference of prefix sums, so count earlier prefixes equal to s − k.arrow_forward
  12. 12Valid Sudoku (LeetCode 36)MediumCheck a partly filled Sudoku for repeats: record each digit as row, column and box keys in one set.arrow_forward
  13. 13LRU Cache (LeetCode 146)MediumBuild a fixed-size cache that evicts the least recently used key: a hash map plus a recency-ordered list.arrow_forward