Arrays & Hashing problems, grouped by pattern
Most interview problems start with an array. The patterns here teach you to avoid the nested loop: keep a running total, move two pointers toward each other, or slide a window across the input so every element is visited once.
When to reach for it
- The input is a list and the brute force is O(n²) or worse
- The question asks about a contiguous subarray or substring
- The array is sorted, or sorting it would not break the answer
Mistakes to watch for
- Shrinking a variable window too late or too early
- Forgetting the empty prefix (sum 0 seen once) in prefix-sum counting
- Off-by-one errors when the window size is fixed
The 195 problems
1Two Sum
- Two Sum Animated Easy
- 3Sum Animated Medium
- Count Good Meals Medium
2Moore's Voting Algorithm
- Majority Element Animated Easy
- Majority Element II Animated Medium
3Kadane's Algorithm
- Kadane's Algorithm - Simple Animated Medium
- Find minimum subarray sum Animated Medium
- Kadane's Algo - Circular Array - No Extra Space Animated Hard
- Gas Station Medium
- Maximum Product Subarray Medium
- Best Sightseeing Pair Medium
4Double Reversal Trick
- Rotate Array Animated Medium
- Reverse Words in a String Animated Medium
5Two Pointer
- Two Sum II - Input Array Is Sorted Animated Medium
- Valid Triangle Number Animated Medium
- Container With Most Water Medium
- Trapping Rain Water Hard
- Valid Palindrome II Easy
- Valid Word Abbreviation Easy
- Merge Sorted Array Easy
- String Compression Medium
- Expressive Words Medium
- Long Pressed Name Easy
- Valid Palindrome Easy
- Backspace String Compare Easy
- Intersection of Two Arrays Easy
- Longest Mountain in Array Medium
- Move Zeroes Easy
- Remove Duplicates from Sorted Array II Medium
- Remove Duplicates from Sorted Array Easy
- Sort Transformed Array Medium
- Squares of a Sorted Array Easy
- Summary Ranges Easy
- One Edit Distance Medium
- Shortest Word Distance Easy
- Valid Mountain Array Easy
- Boats to Save People Medium
- Shortest Subarray to be Removed to Make Array Sorted Medium
- 4Sum Medium
- Break a Palindrome Medium
- Divide Players Into Teams of Equal Skill Medium
- Is Subsequence Easy
- Maximize Distance to Closest Person Medium
- Reverse Only Letters Easy
- Reverse String Easy
- Reverse Words in a String III Easy
- Sort Array By Parity Easy
6Right to Left Traversal
- Array Leaders Animated Easy
- Product of Array Except Self Animated Medium
- Next Permutation Medium
- Buildings With an Ocean View Medium
- Maximum Swap Medium
- Binary Searchable Numbers in an Unsorted Array Medium
- Add Strings Easy
- Roman to Integer Easy
- Add Binary Easy
- Multiply Strings Medium
- Next Greater Element III Medium
- Plus One Easy
7In-place Transformations
- Set Matrix Zeroes Animated Medium
- Find All Duplicates in an Array Animated Medium
- First Missing Positive Hard
- Rotate Image Medium
- Game of Life Medium
8Merge Sort Like Approach
- Count Inversions Animated Medium
- Arranging the array Animated Medium
- Reverse Pairs Hard
- Sort an Array Medium
9Dutch National Flag
- Sort Colors Animated Medium
10Prefix Sum Strategy
- Subarray Sum Equals K Animated Medium
- Subarray with given XOR Animated Medium
- Range Sum Query - Immutable Animated Easy
- Continuous Subarray Sum Medium
- Make Sum Divisible by P Medium
- Number of Wonderful Substrings Medium
- Minimum Penalty for a Shop Medium
- Plates Between Candles Medium
- Minimum Value to Get Positive Step by Step Sum Easy
- Contiguous Array Medium
- Find Pivot Index Easy
11Sliding Window · Fixed
- Maximum Average Subarray I Animated Easy
- K Radius Subarray Averages Animated Medium
- Maximum Number of Vowels in a Substring of Given Length Animated Medium
- Maximum Points You Can Obtain from Cards Medium
- Contains Duplicate II Easy
- Find All Anagrams in a String Medium
- Repeated DNA Sequences Medium
- Alert Using Same Key-Card Three or More Times in a One Hour Period Medium
- High-Access Employees Medium
- Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold Medium
- Permutation in String Medium
12Sliding Window · Variable
- Minimum Size Subarray Sum Animated Medium
- Count of Subarrays with Product Less Than K Animated Medium
- Longest Repeating Character Replacement Medium
- Longest Substring Without Repeating Characters Medium
- Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit Medium
- Max Consecutive Ones III Medium
- Fruit Into Baskets Medium
- Maximum Length Substring With Two Occurrences Easy
- Count Subarrays With Fixed Bounds Hard
- Count Vowel Substrings of a String Easy
- Max Consecutive Ones Easy
- Maximum Number of Visible Points Hard
13Sliding Window · Hash Map
- Number of Substrings Containing All Three Characters Animated Medium
- Minimum Window Substring Animated Hard
- Longest Substring with At Most K Distinct Characters Medium
- Contains Duplicate III Hard
- Maximum Number of Occurrences of a Substring Medium
- Subarrays with K Different Integers Hard
14Hashing
- Group Anagrams Medium
- First Unique Character in a String Easy
- Number of Matching Subsequences Medium
- Group Shifted Strings Medium
- Max Points on a Line Hard
- Valid Anagram Easy
- Invalid Transactions Medium
- Isomorphic Strings Easy
- Number of Black Blocks Medium
- Palindrome Pairs Hard
- Shortest Uncommon Substring in an Array Medium
- Strings Differ by One Character Medium
- Fraction to Recurring Decimal Medium
- Count Elements With Maximum Frequency Easy
- Find The Least Frequent Digit Easy
- Minimum Number of Steps to Make Two Strings Anagram Medium
- Task Scheduler II Medium
- Valid Sudoku Medium
15Matrix Traversal
- Spiral Matrix Medium
- Best Meeting Point Hard
- Diagonal Traverse Medium
- Sparse Matrix Multiplication Medium
16Elimination Recurrence
17Greedy
- Integer to Roman Medium
- Jump Game II Medium
- Jump Game Medium
- Can Place Flowers Easy
- Candy Hard
- Guess the Word Hard
- Make Lexicographically Smallest Array by Swapping Elements Medium
- Minimum Domino Rotations For Equal Row Medium
- Reorganize String Medium
- Task Scheduler Medium
- Minimum Number of Taps to Open to Water a Garden Hard
- Minimize Rounding Error to Meet Target Medium
- Largest Number Medium
- Maximum Product of Three Numbers Easy
- Advantage Shuffle Medium
- Maximum Distance Between Unequal Words in Array I Easy
- Minimum Absolute Difference Easy
- Minimum Number of Coins to be Added Medium
- Minimum Number of Operations to Make Arrays Similar Hard
- Minimum Operations to Make Array Equal II Medium
- Transform Array to All Equal Elements Medium
- Minimum Total Cost to Make Arrays Unequal Hard
- Queue Reconstruction by Height Medium
- Two City Scheduling Medium
- Maximum Units on a Truck Easy
- Partitioning Into Minimum Number Of Deci-Binary Numbers Medium
18Hash Set Lookup
- Longest Consecutive Sequence Medium
- Contains Duplicate Easy
- Fair Candy Swap Easy
- Maximize Area of Square Hole in Grid Medium
19Simulation
- Text Justification Hard
- Integer to English Words Hard
- String to Integer (atoi) Medium
- Remove Comments Medium
- Validate IP Address Medium
- Calculate Amount Paid in Taxes Easy
- Pour Water Medium
- Robot Bounded In Circle Medium
- Candy Crush Medium
- Count and Say Medium
- Fizz Buzz Easy
- Zigzag Conversion Medium
20Contribution Technique
21String Matching (KMP)
22Math / Number Theory
- Bulb Switcher Medium
- Reaching Points Hard
- Water and Jug Problem Medium
- Distribute Candies Among Children II Medium
- Excel Sheet Column Number Easy
- Maximum of Absolute Value Expression Medium
- Count Primes Medium
- Excel Sheet Column Title Easy
- Reverse Integer Medium
- Palindrome Number Easy
23Candidate Generation
24Fenwick Tree (BIT)
25String Basics
26Sorting
- Minimum Time Difference Medium