Skip to content

Introduction to Coding Patterns

Recognize patterns to solve any problem systematically


Why Patterns Matter

The secret to acing coding interviews is not memorizing hundreds of LeetCode problems. Instead, it is about recognizing the underlying patterns that connect them. Recent data from hundreds of real interviews at Google, Meta, Apple, Netflix, and Amazon shows that about 87% of questions are built around only 10 to 12 core problem-solving patterns.

Interviewers are not testing your memory of specific problems. They want to see how well you:

  • Recognize underlying patterns in new problems
  • Apply the correct algorithm quickly and efficiently
  • Communicate your thought process clearly

As one experienced interviewer noted: "One skill that helps most during interview prep is the ability to map a new problem to an already known problem."

The Pattern-Based Approach

Instead of solving problems in isolation:

Traditional ApproachPattern-Based Approach
Solve 500+ random problemsMaster 12-15 core patterns
Memorize solutionsUnderstand underlying techniques
Hope you see a familiar problemRecognize patterns in new problems
Time-consuming and exhaustingEfficient and systematic

Document Structure

This section covers all essential coding patterns you need to master for SDE interviews. Each pattern includes theory, templates, and curated practice problems.

PatternKey InsightCommon ProblemsDifficulty
Two PointersUse two indices to traverse from different positions/directionsTwo Sum (sorted), 3Sum, Container With Most Water, Trapping Rain WaterEasy-Medium
Sliding WindowMaintain a window over contiguous elements for subarray/substring problemsMaximum Subarray, Longest Substring Without Repeating Characters, Minimum Window SubstringMedium
Fast & Slow PointersTwo pointers moving at different speeds to detect cycles or find positionsLinked List Cycle, Find Middle of List, Happy Number, Find Duplicate NumberEasy-Medium
Binary SearchDivide search space in half each iteration for O(logn) lookupSearch in Rotated Sorted Array, Find Peak Element, Koko Eating BananasMedium
BFS (Breadth-First Search)Level-by-level traversal using a queueLevel Order Traversal, Shortest Path, Rotting Oranges, Word LadderMedium
DFS (Depth-First Search)Explore as deep as possible using recursion/stackPath Sum, Number of Islands, Clone Graph, Word SearchMedium
BacktrackingBuild solutions incrementally, abandon paths that fail constraintsPermutations, Combinations, N-Queens, Sudoku SolverMedium-Hard
Dynamic ProgrammingBreak into overlapping subproblems, store and reuse resultsClimbing Stairs, Coin Change, Longest Common Subsequence, Edit DistanceMedium-Hard
Merge IntervalsSort by start time, merge overlapping rangesMerge Intervals, Insert Interval, Meeting Rooms IIMedium
Monotonic StackStack maintaining increasing/decreasing order for next greater/smaller elementNext Greater Element, Daily Temperatures, Largest Rectangle in HistogramMedium-Hard
Union-Find (Disjoint Set)Track connected components with union and find operationsNumber of Connected Components, Redundant Connection, Accounts MergeMedium
Topological SortOrder vertices in a DAG so all edges go from earlier to laterCourse Schedule, Alien Dictionary, Task SchedulingMedium-Hard
Prefix SumPrecompute cumulative sums for O(1) range queriesRange Sum Query, Subarray Sum Equals K, Product of Array Except SelfEasy-Medium
Heap / Priority QueueMaintain min/max element efficiently for top-K problemsKth Largest Element, Merge K Sorted Lists, Find Median from Data StreamMedium
Trie (Prefix Tree)Tree structure for efficient string prefix operationsImplement Trie, Word Search II, Autocomplete SystemMedium-Hard

Pattern Recognition Flowchart

Use this decision tree to identify which pattern to apply based on problem characteristics:

Quick Pattern Recognition Guide

If you see...Think...
"Sorted array" + "find target"Binary Search
"Sorted array" + "pair/triplet"Two Pointers
"Contiguous subarray/substring"Sliding Window
"Fixed-size window"Fixed Sliding Window
"Shortest path" / "minimum steps"BFS
"All paths" / "all combinations"DFS / Backtracking
"Linked list" + "cycle"Fast & Slow Pointers
"Next greater/smaller element"Monotonic Stack
"Top K" / "Kth largest/smallest"Heap
"Overlapping intervals"Merge Intervals
"Connected components"Union-Find or DFS
"Build order" / "prerequisites"Topological Sort
"Prefix/suffix" / "autocomplete"Trie
"Optimal substructure" + "overlapping subproblems"Dynamic Programming
"Subarray sum equals K"Prefix Sum + HashMap

How to Use This Section

For maximum efficiency, study patterns in this order:

Week 1-2: Foundation Patterns

  1. Two Pointers (foundation for many techniques)
  2. Sliding Window (builds on two pointers concept)
  3. Binary Search (essential for efficiency)
  4. Prefix Sum (simple but powerful)

Week 3-4: Graph & Tree Patterns 5. BFS (level-order, shortest path) 6. DFS (path finding, tree traversal) 7. Backtracking (generate all possibilities)

Week 5-6: Advanced Patterns 8. Dynamic Programming (optimization problems) 9. Monotonic Stack (next greater element problems) 10. Heap/Priority Queue (top-K, streaming data)

Week 7-8: Specialized Patterns 11. Union-Find (connectivity problems) 12. Topological Sort (ordering with dependencies) 13. Merge Intervals (scheduling problems) 14. Trie (string problems)

For Each Pattern, You Should

  1. Understand the concept - Read the theory and recognize when to apply it
  2. Study the template - Memorize the generic code structure
  3. Solve example problems - Start with 2-3 classic problems per pattern
  4. Practice variations - Apply the pattern to 5-10 similar problems
  5. Time yourself - Aim to recognize and implement within 20-30 minutes

Each pattern page includes:

  • Concept explanation with visual diagrams
  • Code templates in Python (with Java/C++ alternatives)
  • Time/Space complexity analysis
  • Common variations and edge cases
  • Curated problem list (Easy/Medium/Hard)
  • Interview tips specific to that pattern

Pattern Frequency in Technical Interviews

Based on analysis of reported interview questions from top tech companies, here are the most frequently tested patterns:

Most Common Topics (Per Google Recruiters)

CategoryPatterns/TopicsFrequency
Very HighBFS/DFS/Flood Fill, Binary Search, Hash TablesAsked in 70%+ of interviews
HighTwo Pointers, Sliding Window, Tree TraversalsAsked in 50-70% of interviews
MediumDynamic Programming, Binary Heaps, Union-FindAsked in 30-50% of interviews
OccasionalTrie, Segment Trees, BitmasksAsked in 10-30% of interviews

Top Problem Categories

According to interview experience reports:

  1. Arrays & Strings (30-35%)

    • Two pointers, sliding window, prefix sums
    • String manipulation and parsing
  2. Trees & Graphs (25-30%)

    • BFS/DFS traversals, path finding
    • Tree construction and validation
  3. Dynamic Programming (15-20%)

    • Optimization problems, sequence alignment
    • Grid-based DP problems
  4. System Design Elements (10-15%)

    • Data structure design (LRU Cache, etc.)
    • Algorithm design with constraints
  5. Sorting & Searching (10-15%)

    • Binary search variations
    • Custom sorting with comparators

Interview Tips

  • Communicate constantly: Talk through your thought process as you code
  • Clarify constraints: Ask about input size, edge cases, and expected complexity
  • Start with brute force: Explain the naive approach before optimizing
  • Test your solution: Walk through examples and edge cases
  • Optimize iteratively: Show how patterns improve your initial solution

Additional Resources

Courses & Guides

Books

  • Coding Interview Patterns by Alex Xu & Shaun Gunawardane - 24 patterns with 101 problems
  • Cracking the Coding Interview by Gayle Laakmann McDowell - Classic interview prep

Sources


Next: Start with Two Pointers - the foundation pattern for array problems