Skip to main content

Pattern · 109 problems

Arrays and Hashing Interview Problems

Most first problems hide one question: what have I already seen? A hash map answers it in one lookup.

When I reach for it

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.
  • Order barely matters, but remembering does.

Problems, easiest first

17 easy · 76 medium · 16 hard. Open one to read it, then write it in the free editor.

  1. Best Time to Buy and Sell StockeasySliding Window
  2. Missing NumbereasyBit ManipulationBinary Search
  3. Contains Duplicateeasy
  4. Meeting RoomseasyIntervalsGreedy
  5. Valid AnagrameasyStrings
  6. Two SumeasyTwo Pointers
  7. Happy NumbereasyTwo Pointers
  8. Last Stone WeighteasyHeaps
  9. Min Cost Climbing StairseasyDynamic Programming
  10. Plus Oneeasy
  11. Single NumbereasyBit Manipulation
  12. Binary SearcheasyBinary Search
  13. Flood FilleasyBFS & DFS
  14. Longest Palindrome LengtheasyGreedyStrings
  15. Majority Elementeasy
  16. Ransom NoteeasyStrings
  17. Largest Local Values in a Matrixeasy
  18. 3SummediumTwo Pointers
  19. Combination SummediumBacktracking
  20. Construct Binary Tree from Preorder and Inorder TraversalmediumTreesBFS & DFS
  21. House RobbermediumDynamic Programming
  22. House Robber IImediumDynamic Programming
  23. Implement Trie (Prefix Tree)mediumTreesDesign
  24. Insert IntervalmediumIntervals
  25. Jump GamemediumGreedyDynamic Programming
Show the other 84 problems
  1. Longest Increasing SubsequencemediumBinary SearchDynamic Programming
  2. Longest Repeating Character ReplacementmediumSliding WindowTwo Pointers
  3. Maximum Product SubarraymediumDynamic Programming
  4. Maximum SubarraymediumDynamic Programming
  5. Meeting Rooms IImediumIntervalsGreedy
  6. Merge IntervalsmediumIntervals
  7. Non-overlapping IntervalsmediumIntervalsGreedy
  8. Rotate Imagemedium
  9. Set Matrix Zeroesmedium
  10. Spiral Matrixmedium
  11. Word SearchmediumBacktrackingStrings
  12. Product of Array Except Selfmedium
  13. Coin ChangemediumDynamic ProgrammingBFS & DFS
  14. Container With Most WatermediumGreedyTwo Pointers
  15. Find Minimum in Rotated Sorted ArraymediumBinary Search
  16. Group AnagramsmediumStrings
  17. Longest Consecutive SequencemediumGraphs
  18. Number of IslandsmediumGraphsBFS & DFS
  19. Pacific Atlantic Water FlowmediumGraphsBFS & DFS
  20. Search in Rotated Sorted ArraymediumBinary Search
  21. Telemetry: Top K Frequent Error CodesmediumHeaps
  22. Word BreakmediumTreesDynamic Programming
  23. Car FleetmediumStack
  24. Cheapest Flights Within K StopsmediumHeapsGraphs
  25. Coin Change IImediumDynamic Programming
  26. Combination Sum IImediumBacktracking
  27. Copy List with Random PointermediumLinked Lists
  28. Design TwittermediumLinked ListsHeaps
  29. Detect SquaresmediumDesign
  30. Equal Sum Array PartitionmediumDynamic Programming
  31. Find the Duplicate NumbermediumBit ManipulationBinary Search
  32. Gas StationmediumGreedy
  33. Hand of StraightsmediumGreedy
  34. Jump Game IImediumGreedyDynamic Programming
  35. K Closest Points to OriginmediumBinary SearchHeaps
  36. Kth Largest Element in an ArraymediumBinary SearchHeaps
  37. Letter Combinations of a Phone NumbermediumBacktrackingStrings
  38. LRU CachemediumLinked ListsDesign
  39. Max Area of IslandmediumGraphsBFS & DFS
  40. Merge Triplets to Form Target TripletmediumGreedy
  41. Min Cost to Connect All PointsmediumGreedyHeaps
  42. Partition LabelsmediumGreedyTwo Pointers
  43. PermutationsmediumBacktracking
  44. Redundant ConnectionmediumGraphsBFS & DFS
  45. Rotting OrangesmediumGraphsBFS & DFS
  46. Search a 2D MatrixmediumBinary Search
  47. Stock Trading with CooldownmediumDynamic Programming
  48. SubsetsmediumBit ManipulationBacktracking
  49. Subsets IImediumBit ManipulationBacktracking
  50. Surrounded RegionsmediumGraphsBFS & DFS
  51. Target SummediumBacktrackingDynamic Programming
  52. Task SchedulermediumGreedyHeaps
  53. Valid Sudokumedium
  54. Walls and GatesmediumGraphsBFS & DFS
  55. Daily TemperaturesmediumStack
  56. Evaluate Reverse Polish NotationmediumStack
  57. Koko Eating BananasmediumBinary Search
  58. Permutation in StringmediumSliding WindowTwo Pointers
  59. Two Sum II - Input Array Is SortedmediumTwo Pointers
  60. Time Based Key-Value StoremediumBinary SearchDesign
  61. Accounts MergemediumGraphsStrings
  62. Distance to Nearest ZeromediumBFS & DFS
  63. Find All Anagrams in a StringmediumSliding WindowStrings
  64. Sort ColorsmediumTwo Pointers
  65. K Radius Subarray AveragesmediumSliding Window
  66. Minimum Number of People to TeachmediumGreedy
  67. Task Scheduler IImedium
  68. Asteroid CollisionmediumStack
  69. Word Search IIhardBacktrackingTrees
  70. Alien DictionaryhardGraphsStrings
  71. Find Median from Data StreamhardHeapsTwo Pointers
  72. Minimum Window SubstringhardSliding WindowStrings
  73. Burst BalloonshardDynamic Programming
  74. Longest Increasing Path in a MatrixhardGraphsDynamic Programming
  75. Median of Two Sorted ArrayshardBinary Search
  76. Minimum Interval to Include Each QueryhardIntervalsBinary Search
  77. N-QueenshardBacktracking
  78. Swim in Rising WaterhardBinary SearchHeaps
  79. Word LadderhardGraphsStrings
  80. Largest Rectangle in HistogramhardStack
  81. Sliding Window MaximumhardSliding WindowStack
  82. Trapping Rain WaterhardStackHeaps
  83. Maximum Profit in Job SchedulinghardBinary SearchDynamic Programming
  84. Encrypt and Decrypt StringshardDesignStrings

The idea

Trade memory for speed. Store what you have seen, keyed by the thing you will look up later. Each new element then asks the map one question instead of rescanning the array.

Pick the key before you code. In Two Sum the key is the number you still need. In Group Anagrams it is the sorted letters. The right key is most of the problem.

seen = {}
for i, x in enumerate(items):
    need = target - x          # what would complete x?
    if need in seen:
        return [seen[need], i]
    seen[x] = i                # remember x for later items
Check the map first, then store the current item. That order keeps an element from pairing with itself. Python shown; the idea carries to every language in the editor.

Where it goes wrong

  • Storing the current item before the check lets it match itself.
  • A list cannot be a key. Turn it into a tuple first.
  • Duplicates need a decision: does the map keep the first index or the last?

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

Where Coco comes in

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit, and to talk with her for one hour. After that, talking comes with the $79 pack. Anyone can open the editor and run their code.

Questions people ask

Why does a hash map make these problems faster?
A lookup takes constant time on average, so one pass replaces a nested loop. You pay with memory that grows with the input.
Where should I start with arrays and hashing?
Start with Contains Duplicate, then Valid Anagram, then Two Sum. Each one adds a single idea to the one before it.

Meet these next

Fin and Coco are characters, not people. strongyes.io is independent. Leo builds it with AI agents.