Skip to main content

Pattern · 127 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.

Start with Best Time to Buy and Sell Stock

Free: write the code and run it, no sign-up. Read the problem here; code it on a laptop.

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

19 easy · 89 medium · 19 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. Merge Sorted ArrayeasyTwo Pointers
  18. Move ZeroeseasyTwo Pointers
  19. Largest Local Values in a Matrixeasy
  20. 3SummediumTwo Pointers
  21. Combination SummediumBacktracking
  22. Construct Binary Tree from Preorder and Inorder TraversalmediumTreesBFS & DFS
  23. House RobbermediumDynamic Programming
  24. House Robber IImediumDynamic Programming
  25. Implement Trie (Prefix Tree)mediumTreesDesign
Show the other 102 problems
  1. Insert IntervalmediumIntervals
  2. Jump GamemediumGreedyDynamic Programming
  3. Longest Increasing SubsequencemediumBinary SearchDynamic Programming
  4. Longest Repeating Character ReplacementmediumSliding WindowTwo Pointers
  5. Maximum Product SubarraymediumDynamic Programming
  6. Maximum SubarraymediumDynamic Programming
  7. Meeting Rooms IImediumIntervalsGreedy
  8. Merge IntervalsmediumIntervals
  9. Non-overlapping IntervalsmediumIntervalsGreedy
  10. Rotate Imagemedium
  11. Set Matrix Zeroesmedium
  12. Spiral Matrixmedium
  13. Word SearchmediumBacktrackingStrings
  14. Product of Array Except Selfmedium
  15. Coin ChangemediumDynamic ProgrammingBFS & DFS
  16. Container With Most WatermediumGreedyTwo Pointers
  17. Find Minimum in Rotated Sorted ArraymediumBinary Search
  18. Group AnagramsmediumStrings
  19. Longest Consecutive SequencemediumGraphs
  20. Number of IslandsmediumGraphsBFS & DFS
  21. Pacific Atlantic Water FlowmediumGraphsBFS & DFS
  22. Search in Rotated Sorted ArraymediumBinary Search
  23. Telemetry: Top K Frequent Error CodesmediumHeaps
  24. Word BreakmediumTreesDynamic Programming
  25. Car FleetmediumStack
  26. Cheapest Flights Within K StopsmediumHeapsGraphs
  27. Coin Change IImediumDynamic Programming
  28. Combination Sum IImediumBacktracking
  29. Copy List with Random PointermediumLinked Lists
  30. Design TwittermediumLinked ListsHeaps
  31. Detect SquaresmediumDesign
  32. Equal Sum Array PartitionmediumDynamic Programming
  33. Find the Duplicate NumbermediumBit ManipulationBinary Search
  34. Gas StationmediumGreedy
  35. Hand of StraightsmediumGreedy
  36. Jump Game IImediumGreedyDynamic Programming
  37. K Closest Points to OriginmediumBinary SearchHeaps
  38. Kth Largest Element in an ArraymediumBinary SearchHeaps
  39. Letter Combinations of a Phone NumbermediumBacktrackingStrings
  40. LRU CachemediumLinked ListsDesign
  41. Max Area of IslandmediumGraphsBFS & DFS
  42. Merge Triplets to Form Target TripletmediumGreedy
  43. Min Cost to Connect All PointsmediumGreedyHeaps
  44. Partition LabelsmediumGreedyTwo Pointers
  45. PermutationsmediumBacktracking
  46. Redundant ConnectionmediumGraphsBFS & DFS
  47. Rotting OrangesmediumGraphsBFS & DFS
  48. Search a 2D MatrixmediumBinary Search
  49. Stock Trading with CooldownmediumDynamic Programming
  50. SubsetsmediumBit ManipulationBacktracking
  51. Subsets IImediumBit ManipulationBacktracking
  52. Surrounded RegionsmediumGraphsBFS & DFS
  53. Target SummediumBacktrackingDynamic Programming
  54. Task SchedulermediumGreedyHeaps
  55. Valid Sudokumedium
  56. Walls and GatesmediumGraphsBFS & DFS
  57. Daily TemperaturesmediumStack
  58. Evaluate Reverse Polish NotationmediumStack
  59. Koko Eating BananasmediumBinary Search
  60. Permutation in StringmediumSliding WindowTwo Pointers
  61. Two Sum II - Input Array Is SortedmediumTwo Pointers
  62. Time Based Key-Value StoremediumBinary SearchDesign
  63. Accounts MergemediumGraphsStrings
  64. Distance to Nearest ZeromediumBFS & DFS
  65. Find All Anagrams in a StringmediumSliding WindowStrings
  66. Sort ColorsmediumTwo Pointers
  67. 4SummediumTwo Pointers
  68. Find All Possible Recipes from Given SuppliesmediumGraphsStrings
  69. Insert Delete GetRandom O(1)mediumDesign
  70. K Radius Subarray AveragesmediumSliding Window
  71. Kth Smallest Element in a Sorted MatrixmediumBinary SearchHeaps
  72. Maximum XOR of Two Numbers in an ArraymediumBit Manipulation
  73. Minimum Number of People to TeachmediumGreedy
  74. Next PermutationmediumTwo Pointers
  75. Random Pick with WeightmediumBinary Search
  76. Reorganize StringmediumGreedyStrings
  77. Replace WordsmediumStrings
  78. Rotate ArraymediumTwo Pointers
  79. Search Suggestions SystemmediumBinary SearchStrings
  80. Subarray Sum Equals Kmedium
  81. Task Scheduler IImedium
  82. Top K Frequent WordsmediumHeapsStrings
  83. Asteroid CollisionmediumStack
  84. Word Search IIhardBacktrackingTrees
  85. Alien DictionaryhardGraphsStrings
  86. Find Median from Data StreamhardHeapsTwo Pointers
  87. Minimum Window SubstringhardSliding WindowStrings
  88. Burst BalloonshardDynamic Programming
  89. Longest Increasing Path in a MatrixhardGraphsDynamic Programming
  90. Median of Two Sorted ArrayshardBinary Search
  91. Minimum Interval to Include Each QueryhardIntervalsBinary Search
  92. N-QueenshardBacktracking
  93. Swim in Rising WaterhardBinary SearchHeaps
  94. Word LadderhardGraphsStrings
  95. Largest Rectangle in HistogramhardStack
  96. Sliding Window MaximumhardSliding WindowStack
  97. Trapping Rain WaterhardStackHeaps
  98. Maximum Profit in Job SchedulinghardBinary SearchDynamic Programming
  99. Encrypt and Decrypt StringshardDesignStrings
  100. First Missing Positivehard
  101. LFU CachehardDesign
  102. Sliding Window MedianhardSliding WindowHeaps

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. The editor and Run stay open without an account. 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.