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.
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.
- Best Time to Buy and Sell StockeasySliding Window
- Missing NumbereasyBit ManipulationBinary Search
- Contains Duplicateeasy
- Meeting RoomseasyIntervalsGreedy
- Valid AnagrameasyStrings
- Two SumeasyTwo Pointers
- Happy NumbereasyTwo Pointers
- Last Stone WeighteasyHeaps
- Min Cost Climbing StairseasyDynamic Programming
- Plus Oneeasy
- Single NumbereasyBit Manipulation
- Binary SearcheasyBinary Search
- Flood FilleasyBFS & DFS
- Longest Palindrome LengtheasyGreedyStrings
- Majority Elementeasy
- Ransom NoteeasyStrings
- Merge Sorted ArrayeasyTwo Pointers
- Move ZeroeseasyTwo Pointers
- Largest Local Values in a Matrixeasy
- 3SummediumTwo Pointers
- Combination SummediumBacktracking
- Construct Binary Tree from Preorder and Inorder TraversalmediumTreesBFS & DFS
- House RobbermediumDynamic Programming
- House Robber IImediumDynamic Programming
- Implement Trie (Prefix Tree)mediumTreesDesign
Show the other 102 problems
- Insert IntervalmediumIntervals
- Jump GamemediumGreedyDynamic Programming
- Longest Increasing SubsequencemediumBinary SearchDynamic Programming
- Longest Repeating Character ReplacementmediumSliding WindowTwo Pointers
- Maximum Product SubarraymediumDynamic Programming
- Maximum SubarraymediumDynamic Programming
- Meeting Rooms IImediumIntervalsGreedy
- Merge IntervalsmediumIntervals
- Non-overlapping IntervalsmediumIntervalsGreedy
- Rotate Imagemedium
- Set Matrix Zeroesmedium
- Spiral Matrixmedium
- Word SearchmediumBacktrackingStrings
- Product of Array Except Selfmedium
- Coin ChangemediumDynamic ProgrammingBFS & DFS
- Container With Most WatermediumGreedyTwo Pointers
- Find Minimum in Rotated Sorted ArraymediumBinary Search
- Group AnagramsmediumStrings
- Longest Consecutive SequencemediumGraphs
- Number of IslandsmediumGraphsBFS & DFS
- Pacific Atlantic Water FlowmediumGraphsBFS & DFS
- Search in Rotated Sorted ArraymediumBinary Search
- Telemetry: Top K Frequent Error CodesmediumHeaps
- Word BreakmediumTreesDynamic Programming
- Car FleetmediumStack
- Cheapest Flights Within K StopsmediumHeapsGraphs
- Coin Change IImediumDynamic Programming
- Combination Sum IImediumBacktracking
- Copy List with Random PointermediumLinked Lists
- Design TwittermediumLinked ListsHeaps
- Detect SquaresmediumDesign
- Equal Sum Array PartitionmediumDynamic Programming
- Find the Duplicate NumbermediumBit ManipulationBinary Search
- Gas StationmediumGreedy
- Hand of StraightsmediumGreedy
- Jump Game IImediumGreedyDynamic Programming
- K Closest Points to OriginmediumBinary SearchHeaps
- Kth Largest Element in an ArraymediumBinary SearchHeaps
- Letter Combinations of a Phone NumbermediumBacktrackingStrings
- LRU CachemediumLinked ListsDesign
- Max Area of IslandmediumGraphsBFS & DFS
- Merge Triplets to Form Target TripletmediumGreedy
- Min Cost to Connect All PointsmediumGreedyHeaps
- Partition LabelsmediumGreedyTwo Pointers
- PermutationsmediumBacktracking
- Redundant ConnectionmediumGraphsBFS & DFS
- Rotting OrangesmediumGraphsBFS & DFS
- Search a 2D MatrixmediumBinary Search
- Stock Trading with CooldownmediumDynamic Programming
- SubsetsmediumBit ManipulationBacktracking
- Subsets IImediumBit ManipulationBacktracking
- Surrounded RegionsmediumGraphsBFS & DFS
- Target SummediumBacktrackingDynamic Programming
- Task SchedulermediumGreedyHeaps
- Valid Sudokumedium
- Walls and GatesmediumGraphsBFS & DFS
- Daily TemperaturesmediumStack
- Evaluate Reverse Polish NotationmediumStack
- Koko Eating BananasmediumBinary Search
- Permutation in StringmediumSliding WindowTwo Pointers
- Two Sum II - Input Array Is SortedmediumTwo Pointers
- Time Based Key-Value StoremediumBinary SearchDesign
- Accounts MergemediumGraphsStrings
- Distance to Nearest ZeromediumBFS & DFS
- Find All Anagrams in a StringmediumSliding WindowStrings
- Sort ColorsmediumTwo Pointers
- 4SummediumTwo Pointers
- Find All Possible Recipes from Given SuppliesmediumGraphsStrings
- Insert Delete GetRandom O(1)mediumDesign
- K Radius Subarray AveragesmediumSliding Window
- Kth Smallest Element in a Sorted MatrixmediumBinary SearchHeaps
- Maximum XOR of Two Numbers in an ArraymediumBit Manipulation
- Minimum Number of People to TeachmediumGreedy
- Next PermutationmediumTwo Pointers
- Random Pick with WeightmediumBinary Search
- Reorganize StringmediumGreedyStrings
- Replace WordsmediumStrings
- Rotate ArraymediumTwo Pointers
- Search Suggestions SystemmediumBinary SearchStrings
- Subarray Sum Equals Kmedium
- Task Scheduler IImedium
- Top K Frequent WordsmediumHeapsStrings
- Asteroid CollisionmediumStack
- Word Search IIhardBacktrackingTrees
- Alien DictionaryhardGraphsStrings
- Find Median from Data StreamhardHeapsTwo Pointers
- Minimum Window SubstringhardSliding WindowStrings
- Burst BalloonshardDynamic Programming
- Longest Increasing Path in a MatrixhardGraphsDynamic Programming
- Median of Two Sorted ArrayshardBinary Search
- Minimum Interval to Include Each QueryhardIntervalsBinary Search
- N-QueenshardBacktracking
- Swim in Rising WaterhardBinary SearchHeaps
- Word LadderhardGraphsStrings
- Largest Rectangle in HistogramhardStack
- Sliding Window MaximumhardSliding WindowStack
- Trapping Rain WaterhardStackHeaps
- Maximum Profit in Job SchedulinghardBinary SearchDynamic Programming
- Encrypt and Decrypt StringshardDesignStrings
- First Missing Positivehard
- LFU CachehardDesign
- 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 itemsWhere 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.
