πŸ—ΊοΈ 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:

  1. Follow the patterns in order - they build on each other
  2. Read the theory and templates first
  3. Try each problem yourself for 15-20 minutes before reading the solution
  4. If stuck, read only the approach section (not the code) and try again
  5. After solving, read the dry run and edge cases to solidify understanding

The Patterns

#PatternDocumentKey IdeaProblems
1Two Pointers01_Two_Pointers.mdTwo indices moving through an array - toward each other or in the same direction - to find pairs, partition, or rearrange10
2Binary Search02_Binary_Search.mdHalving the search space each step by exploiting sorted order or a monotonic condition10
3Sliding Window03_Sliding_Window.mdA moving range between two pointers, tracking a running state (sum, frequency, etc.) to avoid redundant computation10
4Prefix Sum04_Prefix_Sum.mdPrecomputing cumulative sums so any range sum can be answered in O(1) via subtraction10
5HashMap / Frequency Counting05_HashMap_Frequency_Counting.mdTrading O(n) space for O(1) lookups - complement search, frequency counting, grouping, and index tracking10
6Kadane’s Algorithm06_Kadanes_Algorithm.mdFinding the maximum (or minimum) subarray sum in one pass using a local vs global max decisionComing soon
7Sorting-Based Patterns07_Sorting_Based.mdSorting as a preprocessing step to simplify pair-finding, interval merging, and greedy decisionsComing soon
8Interval / Merge Intervals08_Intervals.mdSorting intervals by start time and merging or processing overlapsComing soon
9Monotonic Stack09_Monotonic_Stack.mdMaintaining a stack in increasing/decreasing order to efficiently find “next greater/smaller” elementsComing 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)

ProblemPatternLeetCode #
Binary SearchBinary Search704
Two SumHashMap1
Valid PalindromeTwo Pointers125
Running Sum of 1D ArrayPrefix Sum1480
Move ZeroesTwo Pointers283
Maximum Average Subarray ISliding Window643
Search Insert PositionBinary Search35
Squares of a Sorted ArrayTwo Pointers977
First Unique CharacterHashMap387
Merge Sorted ArrayTwo Pointers88

Phase 2: Core Patterns (Easy-Medium)

ProblemPatternLeetCode #
Two Sum IITwo Pointers167
Find First and Last PositionBinary Search34
Remove DuplicatesTwo Pointers26
Valid AnagramHashMap242
Range Sum QueryPrefix Sum303
Find Pivot IndexPrefix Sum724
Contains Duplicate IIHashMap219
Search a 2D MatrixBinary Search74
Minimum Size Subarray SumSliding Window209
Intersection of Two Arrays IIHashMap350

Phase 3: Pattern Mastery (Medium)

ProblemPatternLeetCode #
Container With Most WaterTwo Pointers11
3SumTwo Pointers15
Search in Rotated Sorted ArrayBinary Search33
Longest Substring Without RepeatingSliding Window3
Subarray Sum Equals KPrefix Sum + HashMap560
Group AnagramsHashMap49
Top K Frequent ElementsHashMap347
Longest Consecutive SequenceHashMap/Set128
Find Minimum in Rotated Sorted ArrayBinary Search153
Koko Eating BananasBinary Search on Answer875

Phase 4: Advanced (Medium-Hard)

ProblemPatternLeetCode #
Trapping Rain WaterTwo Pointers42
Minimum Window SubstringSliding Window76
Sliding Window MaximumSliding Window + Deque239
4Sum IIHashMap454
Range Sum Query 2D2D Prefix Sum304
Median of Two Sorted ArraysBinary Search4
Split Array Largest SumBinary Search on Answer410
Capacity to Ship PackagesBinary Search on Answer1011

Each Document Follows This Structure

Every pattern document is organized the same way so you always know what to expect:

  1. What is [Pattern]? - Concept explained from scratch with a concrete example
  2. When to Use It? - Signals and triggers to recognize in problem statements
  3. Types / Variations - Sub-patterns within the main technique
  4. Template Code - Reusable Python templates with comments
  5. 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
  6. 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.