Array Practice Problems
How to Use This Problem List
This list is organized by pattern, not by difficulty. That is intentional.
Most beginners sort by difficulty and try "easy" problems first. The problem is that an easy prefix sum problem and an easy two-pointer problem require completely different thinking. Mixing them teaches you to solve problems randomly rather than recognize patterns.
The better approach: pick one pattern, solve every problem in that section, then move to the next. After finishing a section, you will recognize any problem in that pattern within seconds of reading it.
For each problem, read the title and constraints first. Try to identify the pattern before looking at the hint. If you cannot identify it within 5 minutes, read the hint and try to understand why that pattern applies before coding.
Pattern 1: Single Pass and Basic Traversal
These problems require one traversal of the array with a running variable. The key is knowing what to track at each step.
| Problem | Difficulty | Key Insight |
|---|---|---|
| Find the maximum element | Easy | Track running max, update when current exceeds it |
| Find the minimum element | Easy | Track running min, update when current is smaller |
| Calculate array sum | Easy | Single accumulator, visit every element once |
| Find second largest element | Easy | Track two running values: first and second |
| Count elements greater than average | Easy | Two passes: compute average, then count |
| Find the first missing positive | Medium | Treat array as a hash — place each number at its index |
| Product of array except self | Medium | Prefix products forward, suffix products backward |
| Maximum product subarray | Medium | Track both max and min (negatives can flip sign) |
What makes this pattern: you never need to look back at previous elements or look ahead at future ones. The answer builds from a single left-to-right pass.
Complexity target: O(n) time, O(1) space for most.
Pattern 2: Two Pointers
Two pointers reduce pair-checking from O(n²) to O(n) by eliminating sections of the search space with each comparison.
| Problem | Difficulty | Key Insight |
|---|---|---|
| Two Sum II (sorted array) | Easy | Sort first, move left up when sum too small, right down when too large |
| Remove duplicates from sorted array | Easy | Slow pointer tracks last unique, fast pointer scans forward |
| Move zeros to end | Easy | Write pointer for next non-zero position, read pointer scans |
| Valid palindrome | Easy | Left and right converge, skip non-alphanumeric |
| Reverse an array | Easy | Swap outer elements, converge inward |
| 3Sum | Medium | Fix one element, two-pointer scan for the other two |
| Container with most water | Medium | Move the shorter wall inward — moving the taller can only decrease |
| Trapping rain water | Medium | Water at position i = min(maxLeft, maxRight) - height[i] |
| 4Sum | Medium | Fix two elements with two loops, two-pointer for the inner pair |
| Minimum size subarray sum | Medium | Variable-size two-pointer: expand right, shrink left |
What makes this pattern: two indices, either converging from both ends or moving in the same direction at different speeds. Works best on sorted arrays or when the structure allows you to reason about which pointer to move.
Complexity target: O(n) or O(n log n) with sort.
Pattern 3: Sliding Window
Sliding window maintains a running result across a fixed or variable-size window as it moves from left to right. Each element enters and exits at most once.
| Problem | Difficulty | Key Insight |
|---|---|---|
| Maximum sum subarray of size k | Easy | Fixed window: add right, remove left |
| Average of subarrays of size k | Easy | Maintain running sum, divide by k |
| Longest subarray with sum at most k | Medium | Variable window: expand right, shrink left when violated |
| Longest substring without repeating characters | Medium | Hash set tracks window contents, shrink when duplicate enters |
| Minimum window substring | Hard | Hash map of required chars, shrink when all requirements met |
| Longest repeating character replacement | Medium | Window valid when (length - maxFreq) <= k |
| Find all anagrams in a string | Medium | Fixed window of length p, compare frequency maps |
| Maximum of all subarrays of size k | Hard | Monotonic deque maintains max in O(1) per window |
What makes this pattern: the problem asks for an optimal subarray or substring. The words "contiguous," "subarray," "longest," "shortest," "at most k" are the strongest signals.
Complexity target: O(n) time, O(1) or O(k) space.
Pattern 4: Prefix Sum
Precompute cumulative sums to answer range queries in O(1) after O(n) setup.
| Problem | Difficulty | Key Insight |
|---|---|---|
| Range sum query | Easy | prefix[r] - prefix[l-1] for any range |
| Find pivot index | Easy | leftSum == totalSum - leftSum - arr[i] |
| Subarray sum equals k | Medium | Count prefixes equal to (currentSum - k) |
| Contiguous array (equal 0s and 1s) | Medium | Replace 0 with -1, find longest subarray with sum 0 |
| Subarray sums divisible by k | Medium | Two subarrays with same (prefix % k) have sum divisible by k |
| Number of subarrays with product less than k | Medium | Sliding window variant — prefix products |
| Maximum sum of two non-overlapping subarrays | Hard | Prefix max of fixed-size window sums |
| Count subarrays with score less than k | Hard | Prefix sum with binary search |
What makes this pattern: the problem asks how many subarrays satisfy a sum condition, or queries the sum of arbitrary ranges repeatedly. The word "sum" combined with "equals," "divisible," or "at most" is a strong signal.
Complexity target: O(n) time, O(n) space for the prefix array.
Pattern 5: Sorting and Order
Problems where sorting first unlocks a linear or logarithmic solution that would otherwise be quadratic.
| Problem | Difficulty | Key Insight |
|---|---|---|
| Sort an array (implement) | Easy | Practice merge sort or quick sort from scratch |
| Find kth largest element | Medium | Quick select O(n) average, or min-heap O(n log k) |
| Merge sorted arrays | Easy | Two-pointer merge, similar to merge sort's merge step |
| Merge intervals | Medium | Sort by start, merge overlapping pairs in one pass |
| Non-overlapping intervals | Medium | Sort by end, greedily keep intervals that end earliest |
| Meeting rooms (can one person attend all?) | Easy | Sort by start, check for any overlap |
| Meeting rooms II (min rooms) | Medium | Sort starts and ends separately, two-pointer count |
| Largest number from array elements | Medium | Custom comparator: ab > ba means a comes first |
| Sort colors (Dutch national flag) | Medium | Three-way partition with three pointers |
What makes this pattern: the problem mentions intervals, or finding an order relationship between elements, or becomes obviously simpler if elements are in sorted order.
Complexity target: O(n log n) for sort-based, O(n) for linear partition.
Pattern 6: Binary Search on Arrays
Binary search applies whenever the answer space or the array itself has a monotonic property.
| Problem | Difficulty | Key Insight |
|---|---|---|
| Binary search (standard) | Easy | Eliminate half the remaining space each step |
| Find first and last occurrence | Medium | Binary search twice — once for left, once for right bound |
| Search in rotated sorted array | Medium | One half is always sorted — check which and decide |
| Find minimum in rotated sorted array | Medium | Minimum is where the sorted order breaks |
| Find peak element | Medium | If arr[mid] < arr[mid+1], peak is to the right |
| Search a 2D matrix | Medium | Row binary search then column, or treat as 1D |
| Koko eating bananas | Medium | Binary search on the speed — check feasibility at each mid |
| Minimum capacity to ship packages in D days | Medium | Binary search on capacity — check if D days suffices |
| Find square root (integer) | Easy | Binary search on the answer from 1 to n |
What makes this pattern: the array is sorted or rotated, or the problem says "minimum X such that condition holds" where the condition is monotonic.
Complexity target: O(log n) per search.
Pattern 7: Hash Map and Hash Set
Use when you need fast lookup by value, frequency counting, or detecting membership.
| Problem | Difficulty | Key Insight |
|---|---|---|
| Two Sum (unsorted) | Easy | Hash map: store complement, check before inserting |
| Contains duplicate | Easy | Hash set: return true the moment a value is seen again |
| Find all duplicates | Medium | For each arr[i], negate arr[abs(arr[i])-1]; negatives are duplicates |
| Longest consecutive sequence | Medium | Hash set of all values; start chain only if arr[i]-1 not in set |
| Group anagrams | Medium | Hash map keyed by sorted string or character frequency |
| First missing positive | Medium | Rearrange in-place as hash: arr[i] should be i+1 |
| Subarray with zero sum | Medium | Hash set of prefix sums: seen before means zero-sum subarray |
| Top k frequent elements | Medium | Hash map frequencies, then heap or bucket sort |
| Intersection of two arrays | Easy | Hash set of first array, scan second array for matches |
What makes this pattern: the problem asks about existence, frequency, or pairing where position does not matter — you need to find elements by value, not by index.
Complexity target: O(n) time, O(n) space.
Pattern 8: In-Place Manipulation
Modify the array itself to encode information, avoiding extra space.
| Problem | Difficulty | Key Insight |
|---|---|---|
| Rotate array by k | Medium | Three-reversal technique: reverse all, reverse first k, reverse rest |
| Reverse array | Easy | Two-pointer swap converging inward |
| Remove element in-place | Easy | Write pointer for kept elements, overwrite removed positions |
| Remove duplicates (sorted, in-place) | Easy | Two-pointer: slow tracks last unique, fast scans forward |
| Set matrix zeroes | Medium | Use first row/col as markers; process them last |
| Spiral matrix | Medium | Maintain four boundaries, shrink after each traversal direction |
| Find the duplicate number | Medium | Floyd's cycle detection — treat values as next pointers |
| Missing number | Easy | Sum formula: n*(n+1)/2 minus actual sum |
| Single number | Easy | XOR of all elements — pairs cancel, singleton remains |
What makes this pattern: the problem says "in-place," "O(1) extra space," or the constraint makes allocating a new array impractical.
Complexity target: O(n) time, O(1) space.
Recommended Study Order
If you are starting from scratch, this sequence builds skills progressively without overwhelming you.
Week 1 — Foundation Pattern 1 (Single Pass): all problems Pattern 8 (In-Place, Easy tier): reverse, remove element, missing number, single number Week 2 — Core Patterns Pattern 2 (Two Pointers): start with Easy, then Medium Pattern 3 (Sliding Window): fixed window first, then variable Week 3 — Precomputation and Search Pattern 4 (Prefix Sum): range sum query, subarray sum equals k, pivot index Pattern 6 (Binary Search): standard search, first/last occurrence, rotated array Week 4 — Hash Structures and Harder Problems Pattern 7 (Hash Map/Set): two sum, contains duplicate, longest consecutive Pattern 5 (Sorting): merge intervals, meeting rooms, sort colors Revisit any patterns that felt weak
Problem-Solving Checklist
Before writing any code, run through this checklist:
1. Read the problem until you can restate it without looking. → What is the input? What is the exact output? 2. Work through a concrete example by hand. → At least one normal case and one edge case. 3. Identify the pattern from the problem's signals. → Subarray? → Sliding window or prefix sum → Sorted array? → Binary search or two pointers → Lookup by value? → Hash map → No extra space? → In-place, two pointers 4. State the brute force complexity. → Name the bottleneck before optimizing. 5. Apply the pattern to eliminate the bottleneck. → State why it works, not just that it does. 6. Verify edge cases: empty array, single element, all same values. → Does your solution still produce the correct output? 7. State time and space complexity before submitting. → Read the constraint to confirm your complexity is acceptable.
Difficulty Progression Reference
Use this to gauge where you are and what to tackle next.
| Stage | Characteristics | Target Problems |
|---|---|---|
| Beginner | One loop, one variable, no index tricks | Max, min, sum, reverse |
| Early Intermediate | Two pointers or sliding window, recognizing patterns | Two Sum, remove duplicates, max sum subarray |
| Intermediate | Combining patterns, e.g. sort + two pointers | 3Sum, merge intervals, subarray sum equals k |
| Late Intermediate | In-place tricks, prefix + hash, binary search on answer | Rotate array, trapping rain water, minimum window substring |
| Advanced | Multiple data structures, complex invariants | Maximum sliding window, find duplicate, hard prefix problems |
Common Interview Topics by Company Focus
Different companies weight different areas. This is general guidance based on observed patterns:
Product-based companies (FAANG-tier):
- ›Two Sum variations and hash map patterns appear frequently
- ›Sliding window for substring or subarray optimization
- ›Merge intervals and scheduling problems
- ›Binary search on answer space
Service-based companies and on-campus:
- ›Basic traversal and sorting
- ›Standard binary search
- ›Simple two-pointer problems
- ›Array rotation and reversal
Startup interviews:
- ›Practical problem-solving: prefix sums, frequency counting
- ›In-place manipulation
- ›Combined patterns at medium difficulty
Tracking Your Progress
After solving each problem, record:
- ›Pattern used — which of the 8 patterns does this belong to?
- ›Clue that revealed the pattern — what in the problem statement pointed you there?
- ›Time to solve — set a target of 20-30 minutes for medium problems
- ›Edge cases you missed — were there inputs that broke your first solution?
After 30 to 40 problems, review your notes. Look for which patterns you consistently misidentify and which edge cases you repeatedly miss. That tells you exactly what to focus on next.
The goal is not to solve as many problems as possible. It is to build reliable pattern recognition so that any new problem feels familiar within the first two minutes of reading it.