πΊοΈ Array Patterns - The Complete Study Guide
Welcome
This is a structured, beginner-friendly study guide for mastering array and string patterns - the building blocks of coding interviews. Each pattern has a dedicated deep-dive document with theory, intuition, templates, and 10 LeetCode problems solved step by step.
Who is this for?
- You’re preparing for coding interviews and want a systematic approach
- You can solve easy problems but struggle to recognize which technique to apply
- You want to understand why an approach works, not just memorize solutions
How to use this guide:
- Follow the patterns in order - they build on each other
- Read the theory and templates first
- Try each problem yourself for 15-20 minutes before reading the solution
- If stuck, read only the approach section (not the code) and try again
- After solving, read the dry run and edge cases to solidify understanding
The Patterns
| # | Pattern | Document | Key Idea | Problems |
|---|---|---|---|---|
| 1 | Two Pointers | 01_Two_Pointers.md | Two indices moving through an array - toward each other or in the same direction - to find pairs, partition, or rearrange | 10 |
| 2 | Binary Search | 02_Binary_Search.md | Halving the search space each step by exploiting sorted order or a monotonic condition | 10 |
| 3 | Sliding Window | 03_Sliding_Window.md | A moving range between two pointers, tracking a running state (sum, frequency, etc.) to avoid redundant computation | 10 |
| 4 | Prefix Sum | 04_Prefix_Sum.md | Precomputing cumulative sums so any range sum can be answered in O(1) via subtraction | 10 |
| 5 | HashMap / Frequency Counting | 05_HashMap_Frequency_Counting.md | Trading O(n) space for O(1) lookups - complement search, frequency counting, grouping, and index tracking | 10 |
| 6 | Kadane’s Algorithm | 06_Kadanes_Algorithm.md | Finding the maximum (or minimum) subarray sum in one pass using a local vs global max decision | Coming soon |
| 7 | Sorting-Based Patterns | 07_Sorting_Based.md | Sorting as a preprocessing step to simplify pair-finding, interval merging, and greedy decisions | Coming soon |
| 8 | Interval / Merge Intervals | 08_Intervals.md | Sorting intervals by start time and merging or processing overlaps | Coming soon |
| 9 | Monotonic Stack | 09_Monotonic_Stack.md | Maintaining a stack in increasing/decreasing order to efficiently find “next greater/smaller” elements | Coming soon |
Total: 90 problems across 9 patterns when complete.
How the Patterns Connect
These patterns don’t exist in isolation. Understanding how they relate helps you pick the right tool.
ββββββββββββββββ
β Two Pointers β
ββββββββ¬ββββββββ
β
βββββββββββββββββΌββββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββββββ βββββββββββββββ ββββββββββββββββ
β Binary β β Sliding β β Sorting β
β Search β β Window β β Based β
ββββββββββββββ ββββββββ¬βββββββ ββββββββ¬ββββββββ
β β
β βΌ
β ββββββββββββββββ
β β Intervals β
β ββββββββββββββββ
βΌ
βββββββββββββββ
β Prefix Sum β
ββββββββ¬βββββββ
β
βΌ
βββββββββββββββ
β HashMap / β
β Frequency β
βββββββββββββββ
ββββββββββββββββ βββββββββββββββββββ
β Kadane's β β Monotonic Stack β
β Algorithm β β β
ββββββββββββββββ βββββββββββββββββββ
(builds on prefix sum (often combined with
and sliding window) sliding window)
Key relationships:
- Two Pointers β Binary Search: Binary search uses two pointers (
low,high) that converge on an answer by halving the search space. It’s the “sorted array” specialization of two pointers. - Two Pointers β Sliding Window: Sliding window is two same-direction pointers with a “window state” tracked between them.
- Sliding Window β Prefix Sum: Both avoid recomputation over ranges. Sliding window updates incrementally; prefix sum precomputes everything upfront.
- Prefix Sum β HashMap: Many prefix sum problems use a hashmap to store and look up previous prefix values (e.g., “subarray sum equals k”).
- Sorting β Intervals: Nearly all interval problems start by sorting by start time.
- Kadane’s: Combines ideas from prefix sum (running sum) and sliding window (expand/reset decision).
- Monotonic Stack: Often paired with sliding window for problems like “sliding window maximum.”
Pattern Selection Cheat Sheet
When you see a new problem, use these signals to narrow down the pattern:
Is the input SORTED (or should you sort it)?
β
βββ YES, and looking for a TARGET value or boundary
β βββ Binary Search
β
βββ YES, and looking for PAIRS or comparing ends
β βββ Two Pointers (opposite direction)
β
βββ SORT FIRST, then process
β βββ Intervals / overlaps β Merge Intervals
β βββ Pairs / triplets β Sort + Two Pointers
β βββ Greedy decisions β Sorting-Based
β
βββ NO (unsorted), what are you looking for?
β
βββ CONTIGUOUS subarray / substring with some property?
β βββ Fixed size or longest/shortest β Sliding Window
β βββ Range sum queries β Prefix Sum
β βββ Count subarrays with sum = k β Prefix Sum + HashMap
β βββ Maximum sum subarray β Kadane's Algorithm
β
βββ Finding PAIRS, complements, or duplicates?
β βββ HashMap
β
βββ Rearranging IN-PLACE?
β βββ Two Pointers (same direction / partitioning)
β
βββ "Next greater / smaller element"?
β βββ Monotonic Stack
β
βββ Grouping or frequency analysis?
βββ HashMap / Frequency Counting
Difficulty Progression Across All Patterns
If you’re just starting out, here’s a suggested order of problems across all patterns, grouped by difficulty:
Phase 1: Foundations (Easy)
| Problem | Pattern | LeetCode # |
|---|---|---|
| Binary Search | Binary Search | 704 |
| Two Sum | HashMap | 1 |
| Valid Palindrome | Two Pointers | 125 |
| Running Sum of 1D Array | Prefix Sum | 1480 |
| Move Zeroes | Two Pointers | 283 |
| Maximum Average Subarray I | Sliding Window | 643 |
| Search Insert Position | Binary Search | 35 |
| Squares of a Sorted Array | Two Pointers | 977 |
| First Unique Character | HashMap | 387 |
| Merge Sorted Array | Two Pointers | 88 |
Phase 2: Core Patterns (Easy-Medium)
| Problem | Pattern | LeetCode # |
|---|---|---|
| Two Sum II | Two Pointers | 167 |
| Find First and Last Position | Binary Search | 34 |
| Remove Duplicates | Two Pointers | 26 |
| Valid Anagram | HashMap | 242 |
| Range Sum Query | Prefix Sum | 303 |
| Find Pivot Index | Prefix Sum | 724 |
| Contains Duplicate II | HashMap | 219 |
| Search a 2D Matrix | Binary Search | 74 |
| Minimum Size Subarray Sum | Sliding Window | 209 |
| Intersection of Two Arrays II | HashMap | 350 |
Phase 3: Pattern Mastery (Medium)
| Problem | Pattern | LeetCode # |
|---|---|---|
| Container With Most Water | Two Pointers | 11 |
| 3Sum | Two Pointers | 15 |
| Search in Rotated Sorted Array | Binary Search | 33 |
| Longest Substring Without Repeating | Sliding Window | 3 |
| Subarray Sum Equals K | Prefix Sum + HashMap | 560 |
| Group Anagrams | HashMap | 49 |
| Top K Frequent Elements | HashMap | 347 |
| Longest Consecutive Sequence | HashMap/Set | 128 |
| Find Minimum in Rotated Sorted Array | Binary Search | 153 |
| Koko Eating Bananas | Binary Search on Answer | 875 |
Phase 4: Advanced (Medium-Hard)
| Problem | Pattern | LeetCode # |
|---|---|---|
| Trapping Rain Water | Two Pointers | 42 |
| Minimum Window Substring | Sliding Window | 76 |
| Sliding Window Maximum | Sliding Window + Deque | 239 |
| 4Sum II | HashMap | 454 |
| Range Sum Query 2D | 2D Prefix Sum | 304 |
| Median of Two Sorted Arrays | Binary Search | 4 |
| Split Array Largest Sum | Binary Search on Answer | 410 |
| Capacity to Ship Packages | Binary Search on Answer | 1011 |
Each Document Follows This Structure
Every pattern document is organized the same way so you always know what to expect:
- What is [Pattern]? - Concept explained from scratch with a concrete example
- When to Use It? - Signals and triggers to recognize in problem statements
- Types / Variations - Sub-patterns within the main technique
- Template Code - Reusable Python templates with comments
- Problem Set (10 problems) - Each with:
- Problem statement & LeetCode link
- Clarifying questions & constraints
- Multiple approaches (brute force β optimal) with complexity analysis
- Well-documented Python code
- Edge case discussion
- Step-by-step dry run
- Key Takeaways & Summary - Tables, decision trees, and cheat sheets
Getting Started
Open Two Pointer Guide and begin. Good luck, and remember that the struggle is where the learning happens. Don’t rush to the solutions.
This guide is a work in progress. Patterns 6-9 are coming soon.
