Patterns
Patterns are reusable “mental templates” for solving whole families of problems. Instead of memorizing 200 solutions, learn a handful of patterns that unlock them all.
What Are Patterns?
A pattern tells you three things about any problem:
counts, pointers, min/max, state
expand, contract, scan, split
invariants, constraints satisfied
Key insight: The best candidates don't “guess the trick” — they recognize the pattern, state the invariant, and implement cleanly.
Why Patterns Matter
Patterns aren't just interview tricks. They solve real problems in production systems every day.
Search & Autocomplete
- Sliding window + frequency maps
- Keep results relevant and fast
- Real-time query suggestions
Fraud & Anomaly Detection
- Rolling windows for streaming signals
- Prefix sums for cumulative checks
- "Last seen" maps for pattern detection
Log & Metrics Dashboards
- Two pointers for time-range queries
- Windowed aggregates for KPIs
- Avoid reprocessing everything
User Behavior Analytics
- "Longest streak" → sliding window
- "Most frequent" → hash map counting
- "First time seen" → last seen maps
The Pattern Strategy
Clarify
Ask what matters before jumping in.
- Are we optimizing time, space, or both?
- Can we reorder input?
- Is the input streaming?
- Are duplicates allowed?
Name the Pattern
Say it out loud. Interviewers love this.
- "This is a sliding window because we need a best subarray."
- "This is two pointers because sorted input + monotonic movement."
State the Invariant
The rule that must always be true.
- "Window contains unique characters."
- "Left pointer is the smallest index that still satisfies constraints."
Implement Cleanly
Write the simplest correct version first.
- Reduce nested loops
- Avoid unnecessary recalculation
- Use the right data structure
Core Patterns
Sliding Window
The go-to pattern for "best subarray/substring" problems. Expand right, shrink left, track state.
Use when you see
- "Longest/shortest subarray/substring"
- "At most K…"
- "Contains all…"
- "Maximum sum/average in a range"
Typical tools
Two Pointers
For sorted arrays, pairs, and problems with monotonic movement. Move inward or fast/slow.
Use when you see
- Sorted arrays
- Pairs / triplets
- "Closest to target"
- "Remove duplicates" / "partition"
Typical tools
Hash Map / Last Seen
When you need fast lookup for uniqueness, frequency, or position tracking.
Use when you see
- Uniqueness, frequency, first time
- "Most common"
- Fast lookup needed
Typical tools
Prefix Sums
Precompute cumulative totals for instant range queries and subarray sum problems.
Use when you see
- Range sums
- Subarray sum = K
- Many queries on cumulative totals
Typical tools
Monotonic Stack
Maintain a stack with an invariant for "next greater element" style problems.
Use when you see
- "Next greater element"
- "Daily temperatures"
- Histogram / trapping style shapes
Typical tools
Dynamic Programming
For overlapping subproblems and optimal substructure. State + transitions + memoization.
Use when you see
- Overlapping subproblems
- "Count ways", "min cost", "best score"
- Decisions with state
Typical tools
Classic Problems
Longest Substring Without Repeating Characters
“The Bouncer at the Club”
What you'll learn:
- How to keep a window valid (unique chars)
- When to move left vs right
- Why left = max(left, lastSeen[char] + 1) matters
Common pitfall: Moving left backwards. Once left moves forward, it never goes back.
More pattern problems coming soon. Each includes visual walkthroughs and interview tips.
Meta Pattern Questions
How do I choose the right pattern quickly?
Look at the shape of the requirement. "Best subarray/substring" → Sliding Window. "Sorted + pair/closest" → Two Pointers. "Range sum repeated" → Prefix Sums. "Next greater / skyline" → Monotonic Stack. "Optimal + choices" → DP. If you can state the invariant in one sentence, you're probably on the right track.
What if I start with the wrong pattern?
That's normal. Say it, then pivot: "I tried sliding window, but validity doesn't behave monotonically." or "This needs backtracking/DP because choices branch." Interviewers don't penalize pivots — they penalize silent guessing.
Should I memorize solutions?
Memorize pattern templates, not solutions. The window expand/shrink loop. The two-pointer movement rules. The prefix sum map trick. The monotonic stack invariant. The DP state + transition. Patterns transfer. Copy-pasting solutions does not.
Myths vs Reality
You need to instantly know the trick
Most strong candidates discover the trick by asking the right questions and naming patterns.
Sliding window is only for strings
It's for any 'best range' problem: arrays, streams, logs, metrics, anything.
Hash maps are cheating
They're the point. If the constraint says 'fast lookup', the map is your best friend.
DP is always complicated
Many DP problems are just "make the state obvious" and write transitions cleanly.
Practice Patterns
Pick a pattern and grind 3 reps. You don't need 50 problems a day. You need pattern clarity and consistency.
| Pattern | Practice Focus |
|---|---|
| Sliding Window | unique chars / at most K / min window substring |
| Two Pointers | pair sum / partition / remove duplicates |
| Prefix Sum | subarray sum K / range queries |
| Monotonic Stack | next greater / histogram area |
| DP | climbing stairs / house robber / knapsack-lite |
Practice Pattern Problems
Watch patterns in action with step-by-step visualizations. See how the window expands, how pointers move, and how state evolves.
Open Patterns Visualizer