• A Technical Interview
  • 1 Introduction
    • 1.1 How to Use This Book
    • 1.2 Problem Template
      • 1.2.1 Problem Metadata
      • 1.2.2 Description
      • 1.2.3 Examples
      • 1.2.4 Constraints
      • 1.2.5 Solution
    • 1.3 Glossary of Algorithm
      • 1.3.1 Depth First Traversal
      • 1.3.2 Breadth First Traversal
      • 1.3.3 Topological Sort
      • 1.3.4 Minimax
      • 1.3.5 Recursion
      • 1.3.6 Backtracking
      • 1.3.7 Dynamic Programming
      • 1.3.8 Memoization
      • 1.3.9 Tabulation
      • 1.3.10 Divide and Conquer
      • 1.3.11 Binary Search Tree
      • 1.3.12 Merge sort
      • 1.3.13 Bubble sort
      • 1.3.14 Insertion Sort
      • 1.3.15 Quick Sort
      • 1.3.16 Minimum Heap
      • 1.3.17 Stack
      • 1.3.18 Greedy
      • 1.3.19 Two Pointer
      • 1.3.20 Sliding Window
      • 1.3.21 Hash Table
      • 1.3.22 Binary Search
      • 1.3.23 Sorting
      • 1.3.24 Matrix Traversal
      • 1.3.25 Matrix Manipulation
      • 1.3.26 Array
      • 1.3.27 Linked List
      • 1.3.28 Queue
      • 1.3.29 Trie
      • 1.3.30 Bitwise Operations
      • 1.3.31 Bit Manipulation
      • 1.3.32 Simulation
      • 1.3.33 Probability
      • 1.3.34 Rejection Sampling
      • 1.3.35 Reservoir Sampling
  • 2 Common Strategies
    • 2.1 Two Pointer
      • 2.1.1 Core Concept
      • 2.1.2 Common Patterns
      • 2.1.3 Two Pointer vs Other Techniques
      • 2.1.4 Common Pitfalls
      • 2.1.5 Summary
    • 2.2 Algorithm Selection: Greedy vs Dynamic Programming vs Backtracking
      • 2.2.1 Core Differences
      • 2.2.2 Key Insight: Local vs Global vs Exhaustive
      • 2.2.3 When to Use Greedy
      • 2.2.4 When to Use Dynamic Programming
      • 2.2.5 When to Use Backtracking
      • 2.2.6 The Critical Distinction: Same Problem, Different Questions
      • 2.2.7 Hybrid Cases: DP with Path Reconstruction
      • 2.2.8 Overlapping Subproblems: The Deciding Factor
      • 2.2.9 Decision Tree
      • 2.2.10 Common Mistakes
      • 2.2.11 Summary Table: Which Algorithm for Which Problem?
      • 2.2.12 Key Takeaway
    • 2.3 Sorting Algorithms
      • 2.3.1 Core Concepts
      • 2.3.2 Bubble Sort
      • 2.3.3 Insertion Sort
      • 2.3.4 Merge Sort
      • 2.3.5 Quick Sort
      • 2.3.6 Topological Sort
      • 2.3.7 When to Use Which Sorting Algorithm
    • 2.4 Searching Algorithms
      • 2.4.1 Binary Search
      • 2.4.2 QuickSelect
  • 3 Arrays
    • 3.0.1 Iterating Combinations
    • 3.0.2 N Sum Family Comparison
    • 3.0.3 Greedy Array Patterns
    • 3.1 Two Sums I
      • 3.1.1 Problem Metadata
      • 3.1.2 Description
      • 3.1.3 Examples
      • 3.1.4 Constraints
      • 3.1.5 Solution - Hash Table Approach
      • 3.1.6 Solution - Storing Difference
    • 3.2 Median of Two Sorted Arrays
      • 3.2.1 Problem Metadata
      • 3.2.2 Description
      • 3.2.3 Examples
      • 3.2.4 Constraints
      • 3.2.5 Solution - Binary Elimination (Optimal)
      • 3.2.6 Solution - Two Heaps (Simpler but Suboptimal)
    • 3.3 Container With Most Water
      • 3.3.1 Problem Metadata
      • 3.3.2 Description
      • 3.3.3 Examples
      • 3.3.4 Constraints
      • 3.3.5 Solution - Two Pointers
    • 3.4 Three Sum
      • 3.4.1 Problem Metadata
      • 3.4.2 Description
      • 3.4.3 Examples
      • 3.4.4 Constraints
      • 3.4.5 Solution - Sort + Two Pointers
    • 3.5 3 Sum Closest
      • 3.5.1 Problem Metadata
      • 3.5.2 Description
      • 3.5.3 Examples
      • 3.5.4 Constraints
      • 3.5.5 Solution - Sort + Two Pointers
    • 3.6 4Sum
      • 3.6.1 Problem Metadata
      • 3.6.2 Description
      • 3.6.3 Examples
      • 3.6.4 Constraints
      • 3.6.5 Solution - Nested Two Pointers
    • 3.7 Remove Duplicates from Sorted Array
      • 3.7.1 Problem Metadata
      • 3.7.2 Description
      • 3.7.3 Examples
      • 3.7.4 Constraints
      • 3.7.5 Solution - Two Pointers
    • 3.8 Search in Rotated Sorted Array
      • 3.8.1 Problem Metadata
      • 3.8.2 Description
      • 3.8.3 Examples
      • 3.8.4 Constraints
      • 3.8.5 Solution - Modified Binary Search
    • 3.9 Maximum Subarray
      • 3.9.1 Problem Metadata
      • 3.9.2 Description
      • 3.9.3 Examples
      • 3.9.4 Constraints
      • 3.9.5 Solution - Kadane’s Algorithm
    • 3.10 Merge Intervals
      • 3.10.1 Problem Metadata
      • 3.10.2 Description
      • 3.10.3 Examples
      • 3.10.4 Constraints
      • 3.10.5 Solution - Sort then Merge
    • 3.11 Insert Interval
      • 3.11.1 Problem Metadata
      • 3.11.2 Description
      • 3.11.3 Examples
      • 3.11.4 Constraints
      • 3.11.5 Solution - Linear Merge
    • 3.12 Plus One
      • 3.12.1 Problem Metadata
      • 3.12.2 Description
      • 3.12.3 Examples
      • 3.12.4 Constraints
      • 3.12.5 Solution - Carry Propagation
    • 3.13 Sort Colors
      • 3.13.1 Problem Metadata
      • 3.13.2 Description
      • 3.13.3 Examples
      • 3.13.4 Constraints
      • 3.13.5 Follow-up
      • 3.13.6 Solution - Two Pointers (Two Pass)
    • 3.14 Best Time to Buy and Sell Stock
      • 3.14.1 Problem Metadata
      • 3.14.2 Description
      • 3.14.3 Examples
      • 3.14.4 Constraints
      • 3.14.5 Solution - Track Min Price
    • 3.15 Best Time to Buy and Sell Stock II
      • 3.15.1 Problem Metadata
      • 3.15.2 Description
      • 3.15.3 Examples
      • 3.15.4 Constraints
      • 3.15.5 Solution - Sum Positive Differences
    • 3.16 Gas Station
      • 3.16.1 Problem Metadata
      • 3.16.2 Description
      • 3.16.3 Examples
      • 3.16.4 Constraints
      • 3.16.5 Solution - Greedy One Pass
    • 3.17 Maximum Product Subarray
      • 3.17.1 Problem Metadata
      • 3.17.2 Description
      • 3.17.3 Examples
      • 3.17.4 Constraints
      • 3.17.5 Solution - Track Max and Min Products
    • 3.18 Two Sum II - Input Array Is Sorted
      • 3.18.1 Problem Metadata
      • 3.18.2 Description
      • 3.18.3 Examples
      • 3.18.4 Constraints
      • 3.18.5 Solution - Two Pointers
      • 3.18.6 Solution - Binary Search
    • 3.19 Minimum Size Subarray Sum
      • 3.19.1 Problem Metadata
      • 3.19.2 Description
      • 3.19.3 Examples
      • 3.19.4 Constraints
      • 3.19.5 Solution - Sliding Window
    • 3.20 Product of Array Except Self
      • 3.20.1 Problem Metadata
      • 3.20.2 Description
      • 3.20.3 Examples
      • 3.20.4 Constraints
      • 3.20.5 Follow-up
      • 3.20.6 Solution - Prefix and Suffix Arrays
      • 3.20.7 Solution - Space-Optimized (Follow-up)
    • 3.21 Find the Duplicate Number
      • 3.21.1 Problem Metadata
      • 3.21.2 Description
      • 3.21.3 Examples
      • 3.21.4 Constraints
      • 3.21.5 Follow-up
      • 3.21.6 Solution - Floyd’s Cycle Detection (Tortoise and Hare)
    • 3.22 Random Pick Index
      • 3.22.1 Problem Metadata
      • 3.22.2 Description
      • 3.22.3 Examples
      • 3.22.4 Constraints
      • 3.22.5 Solution - Reservoir Sampling
      • 3.22.6 Alternative Solution - Hash Map (Preprocessing)
      • 3.22.7 Comparison of Solutions
    • 3.23 Random Pick with Weight
      • 3.23.1 Problem Metadata
      • 3.23.2 Description
      • 3.23.3 Examples
      • 3.23.4 Constraints
      • 3.23.5 Solution - Prefix Sum + Binary Search
    • 3.24 Non-overlapping Intervals
      • 3.24.1 Problem Metadata
      • 3.24.2 Description
      • 3.24.3 Examples
      • 3.24.4 Constraints
      • 3.24.5 Solution - Greedy by End Time
    • 3.25 4Sum II
      • 3.25.1 Problem Metadata
      • 3.25.2 Description
      • 3.25.3 Examples
      • 3.25.4 Constraints
      • 3.25.5 Solution - Hash Map Pair Sums
    • 3.26 Binary Search
      • 3.26.1 Problem Metadata
      • 3.26.2 Description
      • 3.26.3 Examples
      • 3.26.4 Constraints
      • 3.26.5 Solution - Binary Search (Recursive)
    • 3.27 Maximize Distance to Closest Person
      • 3.27.1 Problem Metadata
      • 3.27.2 Description
      • 3.27.3 Examples
      • 3.27.4 Constraints
      • 3.27.5 Solution - One-Pass Greedy
    • 3.28 Hand of Straights
      • 3.28.1 Problem Metadata
      • 3.28.2 Description
      • 3.28.3 Examples
      • 3.28.4 Constraints
      • 3.28.5 Solution - Greedy with Sorted TreeMap
      • 3.28.6 Solution - Greedy with HashMap + Stream Sort
    • 3.29 Koko Eating Bananas
      • 3.29.1 Problem Metadata
      • 3.29.2 Description
      • 3.29.3 Examples
      • 3.29.4 Constraints
      • 3.29.5 Solution - Binary Search on Answer
    • 3.30 Sort Array By Parity
      • 3.30.1 Problem Metadata
      • 3.30.2 Description
      • 3.30.3 Examples
      • 3.30.4 Constraints
      • 3.30.5 Solution - Two Pointers
    • 3.31 Interval List Intersections
      • 3.31.1 Problem Metadata
      • 3.31.2 Description
      • 3.31.3 Examples
      • 3.31.4 Constraints
      • 3.31.5 Solution - Two Pointers
    • 3.32 Find Common Characters
      • 3.32.1 Problem Metadata
      • 3.32.2 Description
      • 3.32.3 Examples
      • 3.32.4 Constraints
      • 3.32.5 Solution - Frequency Intersection
    • 3.33 Cutting Ribbons
      • 3.33.1 Problem Metadata
      • 3.33.2 Description
      • 3.33.3 Examples
      • 3.33.4 Constraints
      • 3.33.5 Solution - Binary Search on Answer
    • 3.34 Merge Triplets to Form Target Triplet
      • 3.34.1 Problem Metadata
      • 3.34.2 Description
      • 3.34.3 Examples
      • 3.34.4 Constraints
      • 3.34.5 Solution - Greedy
    • 3.35 Eliminate Maximum Number of Monsters
      • 3.35.1 Problem Metadata
      • 3.35.2 Description
      • 3.35.3 Examples
      • 3.35.4 Constraints
      • 3.35.5 Solution 1
      • 3.35.6 Notes
    • 3.36 Number of Zero-Filled Subarrays
      • 3.36.1 Problem Metadata
      • 3.36.2 Description
      • 3.36.3 Examples
      • 3.36.4 Constraints
      • 3.36.5 Solution - Triangular Number Formula
    • 3.37 Divide Array Into Arrays With Max Difference
      • 3.37.1 Problem Metadata
      • 3.37.2 Description
      • 3.37.3 Examples
      • 3.37.4 Constraints
      • 3.37.5 Solution
    • 3.38 Minimum Operations to Make Binary Array Elements Equal to One I
      • 3.38.1 Problem Metadata
      • 3.38.2 Description
      • 3.38.3 Examples
      • 3.38.4 Constraints
      • 3.38.5 Solution - Greedy Sequential Traversal
      • 3.38.6 Java Code
    • 3.39 Shortest Word Distance
      • 3.39.1 Problem Metadata
      • 3.39.2 Description
      • 3.39.3 Examples
      • 3.39.4 Constraints
      • 3.39.5 Solution - Single Pass
    • 3.40 Count Elements Greater Than Previous Average
      • 3.40.1 Problem Metadata
      • 3.40.2 Description
      • 3.40.3 Examples
      • 3.40.4 Constraints
      • 3.40.5 Solution - Running Sum with Floating Point Average
    • 3.41 Count Number Pairs
      • 3.41.1 Problem Metadata
      • 3.41.2 Description
      • 3.41.3 Examples
      • 3.41.4 Input Format
      • 3.41.5 Output Format
      • 3.41.6 Constraints
      • 3.41.7 Solution - Two Pointers with Sorted Array Optimization
      • 3.41.8 Solution - Optimal Two Pointers (Linear Time)
    • 3.42 Merge and Sort Intervals (HackerRank)
      • 3.42.1 Problem Metadata
      • 3.42.2 Description
      • 3.42.3 Examples
      • 3.42.4 Input Format
      • 3.42.5 Constraints
      • 3.42.6 Solution
    • 3.43 Merge and Sort Intervals with Arrays
      • 3.43.1 Problem Metadata
      • 3.43.2 Description
      • 3.43.3 Examples
      • 3.43.4 Input Format
      • 3.43.5 Constraints
      • 3.43.6 Solution
      • 3.43.7 Solution
    • 3.44 Two Sum II - Boolean
      • 3.44.1 Problem Metadata
      • 3.44.2 Description
      • 3.44.3 Examples
      • 3.44.4 Constraints
      • 3.44.5 Solution - Hash Set
    • 3.45 Two Sum V
      • 3.45.1 Problem Metadata
      • 3.45.2 Description
      • 3.45.3 Examples
      • 3.45.4 Constraints
      • 3.45.5 Solution - Two Pointers
    • 3.46 Max Gain
      • 3.46.1 Problem Metadata
      • 3.46.2 Description
      • 3.46.3 Examples
      • 3.46.4 Constraints
      • 3.46.5 Solution - One-Pass Greedy
    • 3.47 Retrieve Elements K Times
      • 3.47.1 Problem Metadata
      • 3.47.2 Description
      • 3.47.3 Examples
      • 3.47.4 Constraints
      • 3.47.5 Solution - Hash Map Counting
    • 3.48 Maximum Number of Repetitions
      • 3.48.1 Problem Metadata
      • 3.48.2 Description
      • 3.48.3 Examples
      • 3.48.4 Constraints
      • 3.48.5 Solution - In-place Counting
    • 3.49 Selection Sort Array
      • 3.49.1 Problem Metadata
      • 3.49.2 Description
      • 3.49.3 Examples
      • 3.49.4 Constraints
      • 3.49.5 Solution - Iterative Selection
    • 3.50 Quick Sort Array
      • 3.50.1 Problem Metadata
      • 3.50.2 Description
      • 3.50.3 Examples
      • 3.50.4 Constraints
      • 3.50.5 Solution - Lomuto Partitioning
  • 4 Matrix
    • 4.0.1 Definition and Properties
    • 4.0.2 Time and Space Complexity Considerations
    • 4.0.3 Traversal Patterns
    • 4.0.4 Transformation Operations
    • 4.0.5 Algorithmic Techniques
    • 4.1 Valid Sudoku
      • 4.1.1 Problem Metadata
      • 4.1.2 Description
      • 4.1.3 Examples
      • 4.1.4 Constraints
      • 4.1.5 Solution 1 - Three Separate Passes (Brute Force)
      • 4.1.6 Solution 2 - Single Pass with String Keys (Optimized)
    • 4.2 Rotate Image
      • 4.2.1 Problem Metadata
      • 4.2.2 Description
      • 4.2.3 Examples
      • 4.2.4 Constraints
      • 4.2.5 Solution - Transpose + Reverse
    • 4.3 Spiral Matrix
      • 4.3.1 Problem Metadata
      • 4.3.2 Description
      • 4.3.3 Examples
      • 4.3.4 Constraints
      • 4.3.5 Solution - Boundary Simulation
    • 4.4 Set Matrix Zeroes
      • 4.4.1 Problem Metadata
      • 4.4.2 Description
      • 4.4.3 Examples
      • 4.4.4 Constraints
      • 4.4.5 Follow-up
      • 4.4.6 Solution - First Row/Column as Markers
    • 4.5 Search a 2D Matrix
      • 4.5.1 Problem Metadata
      • 4.5.2 Description
      • 4.5.3 Examples
      • 4.5.4 Constraints
      • 4.5.5 Solution - Treat as 1D
      • 4.5.6 Solution 2 - Row-First Linear Search
    • 4.6 Surrounded Regions
      • 4.6.1 Problem Metadata
      • 4.6.2 Description
      • 4.6.3 Examples
      • 4.6.4 Constraints
      • 4.6.5 Solution - Mark Border Regions
      • 4.6.6 Solution 2 - BFS Approach
    • 4.7 Number of Islands
      • 4.7.1 Problem Metadata
      • 4.7.2 Description
      • 4.7.3 Examples
      • 4.7.4 Constraints
      • 4.7.5 Solution - Flood Fill
    • 4.8 Search a 2D Matrix II
      • 4.8.1 Problem Metadata
      • 4.8.2 Description
      • 4.8.3 Examples
      • 4.8.4 Constraints
      • 4.8.5 Solution - Staircase Search
    • 4.9 Walls and Gates
      • 4.9.1 Problem Metadata
      • 4.9.2 Description
      • 4.9.3 Examples
      • 4.9.4 Constraints
      • 4.9.5 Solution - Multi-Source BFS
    • 4.10 Pacific Atlantic Water Flow
      • 4.10.1 Problem Metadata
      • 4.10.2 Description
      • 4.10.3 Examples
      • 4.10.4 Constraints
      • 4.10.5 Solution - DFS from Ocean Borders
    • 4.11 Best Meeting Point
      • 4.11.1 Problem Metadata
      • 4.11.2 Description
      • 4.11.3 Examples
      • 4.11.4 Constraints
      • 4.11.5 Solution - Median Trick
    • 4.12 Design Tic-Tac-Toe
      • 4.12.1 Problem Metadata
      • 4.12.2 Description
      • 4.12.3 Examples
      • 4.12.4 Constraints
      • 4.12.5 Solution - Running Counts
    • 4.13 Battleships in a Board
      • 4.13.1 Problem Metadata
      • 4.13.2 Description
      • 4.13.3 Examples
      • 4.13.4 Constraints
      • 4.13.5 Follow-up
      • 4.13.6 Solution - Top-Left Corner Counting
    • 4.14 01 Matrix
      • 4.14.1 Problem Metadata
      • 4.14.2 Description
      • 4.14.3 Examples
      • 4.14.4 Constraints
      • 4.14.5 Solution 1 - Multi-Source BFS (Matrix Modification)
      • 4.14.6 Solution 2 - Multi-Source BFS (Visited Array - Standard)
    • 4.15 Brick Wall
      • 4.15.1 Problem Metadata
      • 4.15.2 Description
      • 4.15.3 Examples
      • 4.15.4 Constraints
      • 4.15.5 Solution - Hash Map with Prefix Sum
    • 4.16 Rotting Oranges
      • 4.16.1 Problem Metadata
      • 4.16.2 Description
      • 4.16.3 Examples
      • 4.16.4 Constraints
      • 4.16.5 Solution - Multi-Source BFS
    • 4.17 Detect Squares
      • 4.17.1 Problem Metadata
      • 4.17.2 Description
      • 4.17.3 Examples
      • 4.17.4 Constraints
      • 4.17.5 Solution - Hash Map with Frequency Counting
  • 5 Tree
    • 5.0.1 Tree Representation
    • 5.0.2 Breadth First Traversal
    • 5.0.3 Depth First Traversal
    • 5.0.4 Pre-Order Traversal
    • 5.0.5 In-Order Traversal
    • 5.0.6 Post-Order Traversal
    • 5.0.7 Selection Strategy
    • 5.0.8 Binary Search Tree
    • 5.1 Binary Tree Inorder Traversal
      • 5.1.1 Problem Metadata
      • 5.1.2 Description
      • 5.1.3 Examples
      • 5.1.4 Constraints
      • 5.1.5 Solution - Iterative Stack
      • 5.1.6 Solution - Recursive
    • 5.2 Validate Binary Search Tree
      • 5.2.1 Problem Metadata
      • 5.2.2 Description
      • 5.2.3 Examples
      • 5.2.4 Constraints
      • 5.2.5 Solution - Inorder Bounds
    • 5.3 Binary Tree Zigzag Level Order Traversal
      • 5.3.1 Problem Metadata
      • 5.3.2 Description
      • 5.3.3 Examples
      • 5.3.4 Constraints
      • 5.3.5 Solution - BFS with Direction Flag
    • 5.4 Maximum Depth of Binary Tree
      • 5.4.1 Problem Metadata
      • 5.4.2 Description
      • 5.4.3 Examples
      • 5.4.4 Constraints
      • 5.4.5 Solution - DFS Recursion
    • 5.5 Construct Binary Tree from Preorder and Inorder Traversal
      • 5.5.1 Problem Metadata
      • 5.5.2 Description
      • 5.5.3 Examples
      • 5.5.4 Constraints
      • 5.5.5 Solution - Hash Map with Divide and Conquer
    • 5.6 Binary Tree Level Order Traversal II
      • 5.6.1 Problem Metadata
      • 5.6.2 Description
      • 5.6.3 Examples
      • 5.6.4 Constraints
      • 5.6.5 Solution - Reverse BFS
    • 5.7 Convert Sorted Array to BST
      • 5.7.1 Problem Metadata
      • 5.7.2 Description
      • 5.7.3 Examples
      • 5.7.4 Constraints
      • 5.7.5 Solution - Divide and Conquer
    • 5.8 Balanced Binary Tree
      • 5.8.1 Problem Metadata
      • 5.8.2 Description
      • 5.8.3 Examples
      • 5.8.4 Constraints
      • 5.8.5 Solution - Recursive DFS
    • 5.9 Binary Tree Level Order Traversal
      • 5.9.1 Problem Metadata
      • 5.9.2 Description
      • 5.9.3 Examples
      • 5.9.4 Constraints
      • 5.9.5 Solution - BFS with Two Queues
      • 5.9.6 Solution - DFS
    • 5.10 Minimum Depth of Binary Tree
      • 5.10.1 Problem Metadata
      • 5.10.2 Description
      • 5.10.3 Solution - DFS
    • 5.11 Path Sum
      • 5.11.1 Problem Metadata
      • 5.11.2 Description
      • 5.11.3 Examples
      • 5.11.4 Constraints
      • 5.11.5 Solution - DFS with Running Sum
    • 5.12 Path Sum II
      • 5.12.1 Problem Metadata
      • 5.12.2 Description
      • 5.12.3 Solution - Backtracking
    • 5.13 Populating Next Right Pointers in Each Node
      • 5.13.1 Problem Metadata
      • 5.13.2 Description
      • 5.13.3 Solution - Level Traversal
    • 5.14 Binary Tree Preorder Traversal
      • 5.14.1 Problem Metadata
      • 5.14.2 Description
      • 5.14.3 Solution - Iterative Stack
      • 5.14.4 Solution - Recursive
    • 5.15 Binary Tree Postorder Traversal
      • 5.15.1 Problem Metadata
      • 5.15.2 Description
      • 5.15.3 Solution - Iterative Stack
      • 5.15.4 Solution - Recursive
    • 5.16 Implement Trie (Prefix Tree)
      • 5.16.1 Problem Metadata
      • 5.16.2 Description
      • 5.16.3 Examples
      • 5.16.4 Constraints
      • 5.16.5 Solution - TrieNode with HashMap
      • 5.16.6 Solution - Array-Based TrieNode
    • 5.17 Kth Smallest Element in a BST
      • 5.17.1 Problem Metadata
      • 5.17.2 Description
      • 5.17.3 Examples
      • 5.17.4 Constraints
      • 5.17.5 Solution - In-Order Traversal (Recursive)
      • 5.17.6 Solution - Iterative with Stack
    • 5.18 Lowest Common Ancestor of a Binary Search Tree
      • 5.18.1 Problem Metadata
      • 5.18.2 Description
      • 5.18.3 Examples
      • 5.18.4 Constraints
      • 5.18.5 Solution - DFS with Ancestor Checking
      • 5.18.6 Solution - BST Properties (Optimal)
    • 5.19 Two Sum IV - Input is a BST
      • 5.19.1 Problem Metadata
      • 5.19.2 Description
      • 5.19.3 Examples
      • 5.19.4 Constraints
      • 5.19.5 Solution - DFS + HashSet
    • 5.20 Lowest Common Ancestor of a Binary Tree
      • 5.20.1 Problem Metadata
      • 5.20.2 Description
      • 5.20.3 Examples
      • 5.20.4 Constraints
      • 5.20.5 Solution 1 - DFS with Ancestor Checking
      • 5.20.6 Solution 2 - Optimized Post-Order DFS
    • 5.21 Binary Tree Upside Down
      • 5.21.1 Problem Metadata
      • 5.21.2 Description
      • 5.21.3 Solution - Recursive Rotation
    • 5.22 Count Univalue Subtrees
      • 5.22.1 Problem Metadata
      • 5.22.2 Description
      • 5.22.3 Solution - DFS
    • 5.23 Inorder Successor in BST
      • 5.23.1 Problem Metadata
      • 5.23.2 Description
      • 5.23.3 Solution - BST Properties
    • 5.24 Serialize and Deserialize Binary Tree
      • 5.24.1 Problem Metadata
      • 5.24.2 Description
      • 5.24.3 Examples
      • 5.24.4 Constraints
      • 5.24.5 Solution - DFS Preorder Traversal
      • 5.24.6 Solution - BFS Level-Order Traversal
    • 5.25 Binary Tree Longest Consecutive Sequence
      • 5.25.1 Problem Metadata
      • 5.25.2 Description
      • 5.25.3 Solution - DFS Tracking
    • 5.26 Find Leaves of Binary Tree
      • 5.26.1 Problem Metadata
      • 5.26.2 Description
      • 5.26.3 Solution - Height DFS
    • 5.27 Path Sum III
      • 5.27.1 Problem Metadata
      • 5.27.2 Description
      • 5.27.3 Solution - Prefix Sum DFS
    • 5.28 Count Good Nodes in Binary Tree
      • 5.28.1 Problem Metadata
      • 5.28.2 Description
      • 5.28.3 Examples
      • 5.28.4 Constraints
      • 5.28.5 Solution - DFS with Path Maximum
      • 5.28.6 Solution - BFS with Node-Max Wrapper
      • 5.28.7 BFS vs DFS Comparison
    • 5.29 Subtree of Another Tree
      • 5.29.1 Problem Metadata
      • 5.29.2 Description
      • 5.29.3 Examples
      • 5.29.4 Constraints
      • 5.29.5 Solution - DFS with Tree Comparison
    • 5.30 Diameter of Binary Tree
      • 5.30.1 Problem Metadata
      • 5.30.2 Description
      • 5.30.3 Solution - DFS
    • 5.31 Height of Binary Search Tree
      • 5.31.1 Problem Metadata
      • 5.31.2 Description
      • 5.31.3 Examples
      • 5.31.4 Input Format
      • 5.31.5 Constraints
      • 5.31.6 Output Format
      • 5.31.7 Solution - Recursive DFS
    • 5.32 Binary Tree Serialization
      • 5.32.1 Problem Metadata
      • 5.32.2 Description
      • 5.32.3 Examples
      • 5.32.4 Constraints
      • 5.32.5 Solution - Preorder with Null Markers
    • 5.33 Fill in the Ancestors of the Node
      • 5.33.1 Problem Metadata
      • 5.33.2 Description
      • 5.33.3 Examples
      • 5.33.4 Solution - DFS Path Building
    • 5.34 Find the k-th Largest Node in a BST
      • 5.34.1 Problem Metadata
      • 5.34.2 Description
      • 5.34.3 Examples
      • 5.34.4 Constraints
      • 5.34.5 Solution - Reverse Inorder
  • 6 Graph
    • 6.0.1 Undirected Graphs
    • 6.0.2 Directed Acyclic Graph
    • 6.0.3 Weighted Graphs
    • 6.1 Clone Graph
      • 6.1.1 Problem Metadata
      • 6.1.2 Description
      • 6.1.3 Examples
      • 6.1.4 Constraints
      • 6.1.5 Solution - DFS with Map
      • 6.1.6 Solution - BFS with Map
    • 6.2 Course Schedule
      • 6.2.1 Problem Metadata
      • 6.2.2 Description
      • 6.2.3 Examples
      • 6.2.4 Constraints
      • 6.2.5 Solution - Kahn’s Algorithm
      • 6.2.6 Solution - DFS with Color Marking
      • 6.2.7 Solution - DFS with Recursion Stack
      • 6.2.8 Solution - DFS with Post-order Visiting
    • 6.3 Course Schedule II
      • 6.3.1 Problem Metadata
      • 6.3.2 Description
      • 6.3.3 Examples
      • 6.3.4 Constraints
      • 6.3.5 Solution - BFS Order
    • 6.4 Graph Validate Tree
      • 6.4.1 Problem Metadata
      • 6.4.2 Description
      • 6.4.3 Examples
      • 6.4.4 Constraints
      • 6.4.5 Solution - Union Find
      • 6.4.6 Solution - DFS with Parent Tracking
      • 6.4.7 Solution - BFS with Parent Tracking
    • 6.5 Alien Dictionary
      • 6.5.1 Problem Metadata
      • 6.5.2 Description
      • 6.5.3 Examples
      • 6.5.4 Constraints
      • 6.5.5 Solution - Topological Sort with DFS
    • 6.6 Number of Connected Components in an Undirected Graph
      • 6.6.1 Problem Metadata
      • 6.6.2 Description
      • 6.6.3 Examples
      • 6.6.4 Constraints
      • 6.6.5 Solution - DFS Traversal
      • 6.6.6 Solution - BFS Traversal
      • 6.6.7 Solution - Union Find
    • 6.7 Accounts Merge
      • 6.7.1 Problem Metadata
      • 6.7.2 Description
      • 6.7.3 Examples
      • 6.7.4 Constraints
      • 6.7.5 Solution - Union Find
      • 6.7.6 Solution - DFS
    • 6.8 Network Delay Time
      • 6.8.1 Problem Metadata
      • 6.8.2 Description
      • 6.8.3 Examples
      • 6.8.4 Constraints
      • 6.8.5 Solution - Dijkstra’s Algorithm
    • 6.9 Distance of Nearest Cell Having 1
      • 6.9.1 Problem Metadata
      • 6.9.2 Description
      • 6.9.3 Solution - Multi-source BFS
    • 6.10 Graph Serialization
      • 6.10.1 Problem Metadata
      • 6.10.2 Description
      • 6.10.3 Solution - BFS
    • 6.11 Hardware Build Feasibility
      • 6.11.1 Problem Metadata
      • 6.11.2 Description
      • 6.11.3 Examples
      • 6.11.4 Constraints
      • 6.11.5 Solution - Topological Sort (Kahn’s BFS)
      • 6.11.6 Solution - DFS with State Tracking
  • 7 Linked List
    • 7.0.1 Fast and Slow Pointer Technique
    • 7.0.2 Reversal Techniques
    • 7.0.3 Dummy Head Pattern
    • 7.0.4 Two-Pointer Technique
    • 7.0.5 Merge Techniques
    • 7.0.6 In-Place Transformation
    • 7.0.7 Common Patterns Summary
    • 7.0.8 Problem-Solving Checklist
    • 7.1 Add Two Numbers
      • 7.1.1 Problem Metadata
      • 7.1.2 Description
      • 7.1.3 Examples
      • 7.1.4 Constraints
      • 7.1.5 Solution - Iterative Carry Addition
    • 7.2 Remove Nth Node From End of List
      • 7.2.1 Problem Metadata
      • 7.2.2 Description
      • 7.2.3 Examples
      • 7.2.4 Constraints
      • 7.2.5 Solution - Two Pointers with Gap
    • 7.3 Merge Two Sorted Lists
      • 7.3.1 Problem Metadata
      • 7.3.2 Description
      • 7.3.3 Examples
      • 7.3.4 Constraints
      • 7.3.5 Solution - Iterative Merge
    • 7.4 Merge k Sorted Lists
      • 7.4.1 Problem Metadata
      • 7.4.2 Description
      • 7.4.3 Examples
      • 7.4.4 Constraints
      • 7.4.5 Solution - Min-Heap
    • 7.5 Reverse Nodes in k-Group
      • 7.5.1 Problem Metadata
      • 7.5.2 Description
      • 7.5.3 Examples
      • 7.5.4 Constraints
      • 7.5.5 Solution - Iterative Group Reversal
    • 7.6 Remove Duplicates from Sorted List
      • 7.6.1 Problem Metadata
      • 7.6.2 Description
      • 7.6.3 Examples
      • 7.6.4 Constraints
      • 7.6.5 Solution - In-Place Skip
    • 7.7 Reverse Linked List II
      • 7.7.1 Problem Metadata
      • 7.7.2 Description
      • 7.7.3 Examples
      • 7.7.4 Constraints
      • 7.7.5 Solution - Head Insertion
    • 7.8 Copy List with Random Pointer
      • 7.8.1 Problem Metadata
      • 7.8.2 Description
      • 7.8.3 Examples
      • 7.8.4 Constraints
      • 7.8.5 Solution - HashMap (Two-Pass)
      • 7.8.6 Solution - Interleaving Nodes (Space Optimized)
    • 7.9 Linked List Cycle
      • 7.9.1 Problem Metadata
      • 7.9.2 Description
      • 7.9.3 Solution - Floyd’s Cycle Detection
    • 7.10 Linked List Cycle II
      • 7.10.1 Problem Metadata
      • 7.10.2 Description
      • 7.10.3 Solution - Floyd’s Cycle Detection with Entry Point
    • 7.11 Reorder List
      • 7.11.1 Problem Metadata
      • 7.11.2 Description
      • 7.11.3 Solution - Find Middle, Reverse, Merge
    • 7.12 Insertion Sort List
      • 7.12.1 Problem Metadata
      • 7.12.2 Description
      • 7.12.3 Examples
      • 7.12.4 Constraints
      • 7.12.5 Solution - Dummy Head Insertion
    • 7.13 Sort List
      • 7.13.1 Problem Metadata
      • 7.13.2 Description
      • 7.13.3 Examples
      • 7.13.4 Constraints
      • 7.13.5 Solution - Merge Sort on Linked List
    • 7.14 Intersection of Two Linked Lists
      • 7.14.1 Problem Metadata
      • 7.14.2 Solution - Two Pointers
    • 7.15 Reverse Linked List
      • 7.15.1 Problem Metadata
      • 7.15.2 Description
      • 7.15.3 Solution - Iterative Two Pointers
      • 7.15.4 Solution - Recursive
    • 7.16 Palindrome Linked List
      • 7.16.1 Problem Metadata
      • 7.16.2 Solution - Two Pointers
    • 7.17 Odd Even Linked List
      • 7.17.1 Problem Metadata
      • 7.17.2 Description
      • 7.17.3 Solution - Two Pointers
    • 7.18 Merge In Between Linked Lists
      • 7.18.1 Problem Metadata
      • 7.18.2 Description
      • 7.18.3 Examples
      • 7.18.4 Constraints
      • 7.18.5 Solution - Pointer Manipulation
    • 7.19 One-Pass Removal of k-th Node from End
      • 7.19.1 Problem Metadata
      • 7.19.2 Description
      • 7.19.3 Examples
      • 7.19.4 Input Format
      • 7.19.5 Constraints
      • 7.19.6 Output Format
      • 7.19.7 Sample Input 0
      • 7.19.8 Sample Output 0
      • 7.19.9 Sample Input 1
      • 7.19.10 Sample Output 1
      • 7.19.11 Solution - Three Pointers with Forward Search
    • 7.20 Reverse Even-Indexed Nodes and Append
      • 7.20.1 Problem Metadata
      • 7.20.2 Description
      • 7.20.3 Examples
      • 7.20.4 Input Format
      • 7.20.5 Constraints
      • 7.20.6 Output Format
      • 7.20.7 Sample Input 0
      • 7.20.8 Sample Output 0
      • 7.20.9 Sample Input 1
      • 7.20.10 Sample Output 1
      • 7.20.11 Solution - Extract, Reverse, and Append
    • 7.21 Insert Node at Position in Doubly Linked List
      • 7.21.1 Problem Metadata
      • 7.21.2 Description
  • 8 Stack and Queue
    • 8.0.1 Stack Fundamentals
    • 8.0.2 Monotonic Stack Pattern
    • 8.0.3 Parentheses and Bracket Matching
    • 8.0.4 Stack-Based State Tracking
    • 8.0.5 Queue Fundamentals
    • 8.0.6 Implementing Queue Using Stacks
    • 8.0.7 Queue-Based Simulation
    • 8.0.8 Stack vs Queue Decision Guide
    • 8.0.9 Common Stack/Queue Patterns Summary
    • 8.0.10 Problem-Solving Checklist
    • 8.1 Longest Valid Parentheses
      • 8.1.1 Problem Metadata
      • 8.1.2 Description
      • 8.1.3 Examples
      • 8.1.4 Constraints
      • 8.1.5 Solution - Stack
    • 8.2 Evaluate Reverse Polish Notation
      • 8.2.1 Problem Metadata
      • 8.2.2 Description
      • 8.2.3 Examples
      • 8.2.4 Constraints
      • 8.2.5 Solution - Stack
    • 8.3 Min Stack
      • 8.3.1 Problem Metadata
      • 8.3.2 Description
      • 8.3.3 Examples
      • 8.3.4 Constraints
      • 8.3.5 Solution - Single Stack with Paired Values
    • 8.4 Implement Queue using Stacks
      • 8.4.1 Problem Metadata
      • 8.4.2 Description
      • 8.4.3 Examples
      • 8.4.4 Constraints
      • 8.4.5 Solution - Two Stacks with Lazy Transfer
      • 8.4.6 Related Problems
    • 8.5 Task Scheduler
      • 8.5.1 Problem Metadata
      • 8.5.2 Description
      • 8.5.3 Examples
      • 8.5.4 Constraints
      • 8.5.5 Solution - Mathematical Formula
      • 8.5.6 Solution 2 - Greedy Simulation with Max Heap
    • 8.6 Daily Temperatures
      • 8.6.1 Problem Metadata
      • 8.6.2 Description
      • 8.6.3 Examples
      • 8.6.4 Constraints
      • 8.6.5 Solution - Monotonic Decreasing Stack
    • 8.7 Car Fleet
      • 8.7.1 Problem Metadata
      • 8.7.2 Description
      • 8.7.3 Examples
      • 8.7.4 Constraints
      • 8.7.5 Solution - Stack with Sorting
    • 8.8 Maximum Score From Removing Substrings
      • 8.8.1 Problem Metadata
      • 8.8.2 Description
      • 8.8.3 Examples
      • 8.8.4 Constraints
      • 8.8.5 Solution - Greedy with Stack
    • 8.9 Validate Properly Nested Brackets
      • 8.9.1 Metadata
      • 8.9.2 Description
      • 8.9.3 Walkthrough
      • 8.9.4 Analysis
      • 8.9.5 Implementation Steps
      • 8.9.6 Java Code
    • 8.10 Next Greater Element with Position Offset
      • 8.10.1 Metadata
      • 8.10.2 Description
      • 8.10.3 Walkthrough
      • 8.10.4 Analysis
      • 8.10.5 Implementation Steps
      • 8.10.6 Java Code
    • 8.11 Lock-Free Stack Using Linked List (CAS)
      • 8.11.1 Problem Metadata
      • 8.11.2 Description
      • 8.11.3 Examples
      • 8.11.4 Constraints
      • 8.11.5 Solution - Linked List Head as Top
  • 9 Heap
    • 9.0.1 Overview
    • 9.0.2 Core Operations
    • 9.0.3 Implementation - Golang
    • 9.0.4 Implementation - Java
    • 9.0.5 Common Patterns
    • 9.0.6 Min Heap vs Max Heap Comparison
    • 9.1 Find k-th Largest Element in an Array
      • 9.1.1 Problem Metadata
      • 9.1.2 Description
      • 9.1.3 Examples
      • 9.1.4 Constraints
      • 9.1.5 Solution - Size-k Min Heap
    • 9.2 Meeting Rooms II
      • 9.2.1 Problem Metadata
      • 9.2.2 Description
      • 9.2.3 Examples
      • 9.2.4 Constraints
      • 9.2.5 Solution - Two-Pointer Sweep
      • 9.2.6 Solution - Min-Heap
    • 9.3 Find Median from Data Stream
      • 9.3.1 Problem Metadata
      • 9.3.2 Description
      • 9.3.3 Examples
      • 9.3.4 Constraints
      • 9.3.5 Solution - Two Heaps
    • 9.4 Top K Frequent Elements
      • 9.4.1 Problem Metadata
      • 9.4.2 Description
      • 9.4.3 Examples
      • 9.4.4 Constraints
      • 9.4.5 Solution - Min-Heap Approach
      • 9.4.6 Solution - Sorting with Stream API
      • 9.4.7 Solution - QuickSelect (Optimal)
    • 9.5 K Closest Points to Origin
      • 9.5.1 Problem Metadata
      • 9.5.2 Description
      • 9.5.3 Examples
      • 9.5.4 Constraints
      • 9.5.5 Solution - Max Heap
      • 9.5.6 Solution - Sort + Stream API
      • 9.5.7 Solution - QuickSelect
    • 9.6 Last Stone Weight
      • 9.6.1 Problem Metadata
      • 9.6.2 Description
      • 9.6.3 Examples
      • 9.6.4 Constraints
      • 9.6.5 Solution - Max Heap
    • 9.7 Top K Frequent Events with Order Preservation (HackerRank)
      • 9.7.1 Problem Metadata
      • 9.7.2 Description
      • 9.7.3 Examples
      • 9.7.4 Input Format
      • 9.7.5 Output Format
      • 9.7.6 Constraints
      • 9.7.7 Solution
    • 9.8 Merge K Sorted Arrays
      • 9.8.1 Problem Metadata
      • 9.8.2 Description
      • 9.8.3 Examples
      • 9.8.4 Constraints
      • 9.8.5 Solution - Min Heap of Array Entries
    • 9.9 Top K Elements
      • 9.9.1 Problem Metadata
      • 9.9.2 Description
      • 9.9.3 Examples
      • 9.9.4 Constraints
      • 9.9.5 Solution 1 - Sort Then Slice
      • 9.9.6 Solution 2 - Partial Bubble (k Passes)
      • 9.9.7 Solution 3 - Min Heap of Size k
    • 9.10 VIP Customer Scheduler
      • 9.10.1 Metadata
      • 9.10.2 Description
      • 9.10.3 Walkthrough
      • 9.10.4 Analysis
      • 9.10.5 Implementation Steps
      • 9.10.6 Java Code
      • 9.10.7 Key Insights
  • 10 String Manipulation
    • 10.0.1 Sliding Window Pattern
    • 10.0.2 Two-Pointer Technique
    • 10.0.3 Character Frequency with HashMap
    • 10.0.4 Anagram Detection Techniques
    • 10.0.5 String Building and Concatenation
    • 10.0.6 String Hashing
    • 10.0.7 Common String Patterns Summary
    • 10.0.8 Problem-Solving Checklist
    • 10.1 Longest Substring Without Repeating Characters
      • 10.1.1 Problem Metadata
      • 10.1.2 Description
      • 10.1.3 Examples
      • 10.1.4 Constraints
      • 10.1.5 Solution - Sliding Window Set
    • 10.2 String to Integer (atoi)
      • 10.2.1 Problem Metadata
      • 10.2.2 Description
      • 10.2.3 Examples
      • 10.2.4 Constraints
      • 10.2.5 Solution - Character-by-Character Parsing
    • 10.3 Longest Common Prefix
      • 10.3.1 Problem Metadata
      • 10.3.2 Description
      • 10.3.3 Examples
      • 10.3.4 Constraints
      • 10.3.5 Solution - Column Scan
    • 10.4 Group Anagrams
      • 10.4.1 Problem Metadata
      • 10.4.2 Description
      • 10.4.3 Examples
      • 10.4.4 Constraints
      • 10.4.5 Solution - Hash Signature Map
    • 10.5 Word Ladder
      • 10.5.1 Problem Metadata
      • 10.5.2 Description
      • 10.5.3 Examples
      • 10.5.4 Constraints
      • 10.5.5 Solution - BFS Over Word Mutations
    • 10.6 Design Add and Search Words Data Structure
      • 10.6.1 Problem Metadata
      • 10.6.2 Description
      • 10.6.3 Examples
      • 10.6.4 Constraints
      • 10.6.5 Solution - Trie with DFS
    • 10.7 Encode and Decode Strings
      • 10.7.1 Problem Metadata
      • 10.7.2 Description
      • 10.7.3 Examples
      • 10.7.4 Constraints
      • 10.7.5 Solution - Length Prefix Encoding
    • 10.8 Ransom Note
      • 10.8.1 Problem Metadata
      • 10.8.2 Description
      • 10.8.3 Examples
      • 10.8.4 Constraints
      • 10.8.5 Solution - Frequency Counting
    • 10.9 Longest Palindrome
      • 10.9.1 Problem Metadata
      • 10.9.2 Description
      • 10.9.3 Examples
      • 10.9.4 Constraints
      • 10.9.5 Solution - Greedy Frequency Counting
    • 10.10 Longest Repeating Character Replacement
      • 10.10.1 Problem Metadata
      • 10.10.2 Description
      • 10.10.3 Examples
      • 10.10.4 Constraints
      • 10.10.5 Solution - Sliding Window
    • 10.11 Find All Anagrams in a String
      • 10.11.1 Problem Metadata
      • 10.11.2 Description
      • 10.11.3 Examples
      • 10.11.4 Constraints
      • 10.11.5 Solution 1 - Brute Force with Frequency Map
      • 10.11.6 Solution 2 - Sliding Window with Frequency Map
    • 10.12 Permutation in String
      • 10.12.1 Problem Metadata
      • 10.12.2 Description
      • 10.12.3 Examples
      • 10.12.4 Constraints
      • 10.12.5 Solution 1 - Brute Force with Frequency Map
      • 10.12.6 Solution 2 - Sliding Window with Frequency Map
      • 10.12.7 Solution Comparison
    • 10.13 Valid Palindrome II
      • 10.13.1 Problem Metadata
      • 10.13.2 Description
      • 10.13.3 Examples
      • 10.13.4 Constraints
      • 10.13.5 Solution - Two Pointers with One Skip Allowed
    • 10.14 Sort Characters By Frequency
      • 10.14.1 Problem Metadata
      • 10.14.2 Description
      • 10.14.3 Examples
      • 10.14.4 Constraints
      • 10.14.5 Solution - Frequency Map with Stream Sorting
    • 10.15 Verifying an Alien Dictionary
      • 10.15.1 Problem Metadata
      • 10.15.2 Description
      • 10.15.3 Examples
      • 10.15.4 Constraints
      • 10.15.5 Solution - Pairwise Comparison with Rank Map
    • 10.16 Minimum Number of Steps to Make Two Strings Anagram
      • 10.16.1 Problem Metadata
      • 10.16.2 Description
      • 10.16.3 Examples
      • 10.16.4 Constraints
      • 10.16.5 Solution
    • 10.17 Number of Substrings Containing All Three Characters
      • 10.17.1 Problem Metadata
      • 10.17.2 Description
      • 10.17.3 Examples
      • 10.17.4 Constraints
      • 10.17.5 Solution - Sliding Window
    • 10.18 Check Palindrome by Filtering Non-Letters
      • 10.18.1 Problem Metadata
      • 10.18.2 Description
      • 10.18.3 Examples
      • 10.18.4 Constraints
      • 10.18.5 Solution - Two Pointers with Filtering
    • 10.19 Max Unique Substring Length in a Session
      • 10.19.1 Problem Metadata
      • 10.19.2 Description
      • 10.19.3 Examples
      • 10.19.4 Constraints
      • 10.19.5 Solution - Split Sessions and Sliding Window
    • 10.20 Binary Representation
      • 10.20.1 Problem Metadata
      • 10.20.2 Description
      • 10.20.3 Examples
      • 10.20.4 Constraints
      • 10.20.5 Solution - Iterative Division
    • 10.21 First Non-Repeated Character
      • 10.21.1 Problem Metadata
      • 10.21.2 Description
      • 10.21.3 Examples
      • 10.21.4 Constraints
      • 10.21.5 Solution - Frequency Counting
    • 10.22 Insert Stars
      • 10.22.1 Problem Metadata
      • 10.22.2 Description
      • 10.22.3 Examples
      • 10.22.4 Constraints
      • 10.22.5 Solution - Recursive Pair Expansion
  • 11 Math
    • 11.0.1 Bit Manipulation
    • 11.0.2 Digit Extraction and Manipulation
    • 11.0.3 Fast Exponentiation
    • 11.0.4 Number Theory
    • 11.0.5 Probability and Rejection Sampling
    • 11.1 Reverse Integer
      • 11.1.1 Problem Metadata
      • 11.1.2 Description
      • 11.1.3 Examples
      • 11.1.4 Constraints
      • 11.1.5 Solution - Digit Extraction with Overflow Detection
    • 11.2 Number of 1 Bits
      • 11.2.1 Problem Metadata
      • 11.2.2 Description
      • 11.2.3 Examples
      • 11.2.4 Constraints
      • 11.2.5 Solution 1 - Digit Extraction with Modulo
      • 11.2.6 Solution 2 - Brian Kernighan’s Algorithm
    • 11.3 Pow(x, n)
      • 11.3.1 Problem Metadata
      • 11.3.2 Description
      • 11.3.3 Examples
      • 11.3.4 Constraints
      • 11.3.5 Solution - Fast Exponentiation
    • 11.4 Single Number
      • 11.4.1 Problem Metadata
      • 11.4.2 Description
      • 11.4.3 Examples
      • 11.4.4 Constraints
      • 11.4.5 Solution 1 - HashSet
      • 11.4.6 Solution 2 - XOR Bit Manipulation (Optimal)
    • 11.5 Reverse Bits
      • 11.5.1 Problem Metadata
      • 11.5.2 Description
      • 11.5.3 Examples
      • 11.5.4 Constraints
      • 11.5.5 Follow-up
      • 11.5.6 Solution 1 - String Reversal
      • 11.5.7 Solution 2 - Bit-by-Bit Reversal (Optimal)
    • 11.6 Happy Number
      • 11.6.1 Problem Metadata
      • 11.6.2 Description
      • 11.6.3 Examples
      • 11.6.4 Constraints
      • 11.6.5 Solution 1 - Hash Set Cycle Detection
      • 11.6.6 Solution 2 - Floyd’s Cycle Detection (Two Pointers)
    • 11.7 Count Primes
      • 11.7.1 Problem Metadata
      • 11.7.2 Description
      • 11.7.3 Examples
      • 11.7.4 Constraints
      • 11.7.5 Solution - Sieve of Eratosthenes
    • 11.8 Sum of Two Integers
      • 11.8.1 Problem Metadata
      • 11.8.2 Description
      • 11.8.3 Examples
      • 11.8.4 Constraints
      • 11.8.5 Solution - Bitwise Addition
    • 11.9 Implement Rand10() Using Rand7()
      • 11.9.1 Problem Metadata
      • 11.9.2 Description
      • 11.9.3 Examples
      • 11.9.4 Constraints
      • 11.9.5 Follow-up
      • 11.9.6 Solution - Rejection Sampling
    • 11.10 Power of Two
      • 11.10.1 Problem Metadata
      • 11.10.2 Description
      • 11.10.3 Examples
      • 11.10.4 Constraints
      • 11.10.5 Solution 1 - Recursive Halving
      • 11.10.6 Solution 2 - Bitwise Trick
  • 12 Interactive Problems
    • 12.1 First Bad Version
      • 12.1.1 Problem Metadata
      • 12.1.2 Description
      • 12.1.3 Examples
      • 12.1.4 Constraints
      • 12.1.5 Solution - Binary Search
    • 12.2 Guess the Word
      • 12.2.1 Problem Metadata
      • 12.2.2 Description
      • 12.2.3 Examples
      • 12.2.4 Constraints
      • 12.2.5 Solution - Minimax Filtering
  • 13 Advanced Data Structure
    • 13.1 Design LRU Cache
      • 13.1.1 Problem Metadata
      • 13.1.2 Description
      • 13.1.3 Examples
      • 13.1.4 Constraints
      • 13.1.5 Solution 1 - Hash Map + Doubly Linked List
      • 13.1.6 Solution 2 - LinkedHashMap
    • 13.2 Design Twitter
      • 13.2.1 Problem Metadata
      • 13.2.2 Description
      • 13.2.3 Examples
      • 13.2.4 Constraints
      • 13.2.5 Solution 1 - Min-Heap of Size 10 (Per-User Bucket)
      • 13.2.6 Solution 2 - k-way Merge (Max-Heap with Lazy Loading)
    • 13.3 Design HashSet
      • 13.3.1 Problem Metadata
      • 13.3.2 Description
      • 13.3.3 Examples
      • 13.3.4 Constraints
      • 13.3.5 Solution - Separate Chaining Buckets
    • 13.4 Design HashMap
      • 13.4.1 Problem Metadata
      • 13.4.2 Description
      • 13.4.3 Examples
      • 13.4.4 Constraints
      • 13.4.5 Solution - Linked List Buckets
    • 13.5 My Calendar I
      • 13.5.1 Problem Metadata
      • 13.5.2 Description
      • 13.5.3 Examples
      • 13.5.4 Constraints
      • 13.5.5 Solution - Brute Force Interval Scan
      • 13.5.6 Solution - TreeMap Boundary Search
    • 13.6 Time Based Key-Value Store
      • 13.6.1 Problem Metadata
      • 13.6.2 Description
      • 13.6.3 Examples
      • 13.6.4 Constraints
      • 13.6.5 Solution 1 - Hash Map with ArrayList + Binary Search
      • 13.6.6 Solution 2 - Hash Map with TreeMap
      • 13.6.7 Solution 3 - ArrayList with Binary Insertion (For Non-Increasing Timestamps)
    • 13.7 Simple Bank System
      • 13.7.1 Problem Metadata
      • 13.7.2 Description
      • 13.7.3 Examples
      • 13.7.4 Constraints
      • 13.7.5 Solution - Array-Based Account Storage
    • 13.8 Implement Queue
      • 13.8.1 Problem Metadata
      • 13.8.2 Description
      • 13.8.3 Examples
      • 13.8.4 Constraints
      • 13.8.5 Solution 1 - Circular Array
      • 13.8.6 Solution 2 - Linked List
      • 13.8.7 Solution 3 - Two Stacks
      • 13.8.8 Related Problems
    • 13.9 Implement Stack Using Queues
      • 13.9.1 Problem Metadata
      • 13.9.2 Description
      • 13.9.3 Examples
      • 13.9.4 Constraints
      • 13.9.5 Solution - Single Queue Rotation
  • 14 Dynamic Programming and Backtracking
    • 14.0.1 Dynamic Programming
    • 14.0.2 Backtracking Algorithms
    • 14.1 Longest Palindromic Substring
      • 14.1.1 Problem Metadata
      • 14.1.2 Description
      • 14.1.3 Examples
      • 14.1.4 Constraints
      • 14.1.5 Solution - DP via Reversed String
    • 14.2 Letter Combinations of a Phone Number
      • 14.2.1 Problem Metadata
      • 14.2.2 Description
      • 14.2.3 Examples
      • 14.2.4 Constraints
      • 14.2.5 Solution - Backtracking
    • 14.3 Combination Sum
      • 14.3.1 Problem Metadata
      • 14.3.2 Description
      • 14.3.3 Examples
      • 14.3.4 Constraints
      • 14.3.5 Solution - Dynamic Programming (Unbounded Knapsack)
    • 14.4 Permutations
      • 14.4.1 Problem Metadata
      • 14.4.2 Description
      • 14.4.3 Examples
      • 14.4.4 Constraints
      • 14.4.5 Solution - Backtracking with Choose-Explore-Unchoose Pattern
      • 14.4.6 Solution - Backtracking with Visited Array
    • 14.5 Permutations II
      • 14.5.1 Problem Metadata
      • 14.5.2 Description
      • 14.5.3 Examples
      • 14.5.4 Constraints
      • 14.5.5 Solution - Backtracking with Choose-Explore-Unchoose Pattern
      • 14.5.6 Solution - Backtracking with Visited Array
    • 14.6 Unique Paths
      • 14.6.1 Problem Metadata
      • 14.6.2 Description
      • 14.6.3 Examples
      • 14.6.4 Constraints
      • 14.6.5 Solution - DP Grid
    • 14.7 Minimum Path Sum
      • 14.7.1 Problem Metadata
      • 14.7.2 Description
      • 14.7.3 Examples
      • 14.7.4 Constraints
      • 14.7.5 Solution - DP Table
    • 14.8 Climbing Stairs
      • 14.8.1 Problem Metadata
      • 14.8.2 Description
      • 14.8.3 Examples
      • 14.8.4 Constraints
      • 14.8.5 Solution - DP Tabulation
      • 14.8.6 Solution - DP Memoization
    • 14.9 Subsets
      • 14.9.1 Problem Metadata
      • 14.9.2 Description
      • 14.9.3 Examples
      • 14.9.4 Constraints
      • 14.9.5 Solution - Backtracking
    • 14.10 Word Search
      • 14.10.1 Problem Metadata
      • 14.10.2 Description
      • 14.10.3 Examples
      • 14.10.4 Constraints
      • 14.10.5 Solution - DFS Backtracking
    • 14.11 Subsets II
      • 14.11.1 Problem Metadata
      • 14.11.2 Description
      • 14.11.3 Examples
      • 14.11.4 Constraints
      • 14.11.5 Solution - Backtracking with Skip Logic
    • 14.12 Decode Ways
      • 14.12.1 Problem Metadata
      • 14.12.2 Description
      • 14.12.3 Examples
      • 14.12.4 Constraints
      • 14.12.5 Solution - Bottom-Up Dynamic Programming (Tabulation)
      • 14.12.6 Solution 2 - Top-Down Dynamic Programming (Memoization)
    • 14.13 Pascal’s Triangle
      • 14.13.1 Problem Metadata
      • 14.13.2 Description
      • 14.13.3 Examples
      • 14.13.4 Constraints
      • 14.13.5 Solution - Dynamic Programming (Tabulation)
    • 14.14 Word Break
      • 14.14.1 Problem Metadata
      • 14.14.2 Description
      • 14.14.3 Examples
      • 14.14.4 Constraints
      • 14.14.5 Solution 1 - Recursive Prefix Search (Top-Down DP with Memoization)
      • 14.14.6 Solution 2 - Bottom-Up Dynamic Programming
      • 14.14.7 Solution 3 - Trie-Assisted DP
    • 14.15 House Robber
      • 14.15.1 Problem Metadata
      • 14.15.2 Description
      • 14.15.3 Examples
      • 14.15.4 Constraints
      • 14.15.5 Solution - Linear DP
    • 14.16 Maximal Square
      • 14.16.1 Problem Metadata
      • 14.16.2 Description
      • 14.16.3 Examples
      • 14.16.4 Constraints
      • 14.16.5 Solution - Dynamic Programming
    • 14.17 Paint House
      • 14.17.1 Problem Metadata
      • 14.17.2 Description
      • 14.17.3 Examples
      • 14.17.4 Constraints
      • 14.17.5 Solution - Rolling DP
    • 14.18 Longest Increasing Subsequence
      • 14.18.1 Problem Metadata
      • 14.18.2 Description
      • 14.18.3 Examples
      • 14.18.4 Constraints
      • 14.18.5 Solution - Binary Search (Patience Sorting)
      • 14.18.6 Solution - Dynamic Programming
    • 14.19 Best Time to Buy and Sell Stock with Cooldown
      • 14.19.1 Problem Metadata
      • 14.19.2 Description
      • 14.19.3 Examples
      • 14.19.4 Constraints
      • 14.19.5 Solution - State Machine DP
    • 14.20 Coin Change
      • 14.20.1 Problem Metadata
      • 14.20.2 Description
      • 14.20.3 Examples
      • 14.20.4 Constraints
      • 14.20.5 Solution - Dynamic Programming (Unbounded Knapsack)
    • 14.21 Coin Change II
      • 14.21.1 Problem Metadata
      • 14.21.2 Description
      • 14.21.3 Examples
      • 14.21.4 Constraints
      • 14.21.5 Solution - Dynamic Programming (Unbounded Knapsack)
    • 14.22 Palindromic Substrings
      • 14.22.1 Problem Metadata
      • 14.22.2 Description
      • 14.22.3 Examples
      • 14.22.4 Constraints
      • 14.22.5 Solution 1 - Expand Around Center
      • 14.22.6 Solution 2 - Dynamic Programming (Tabulation)
    • 14.23 Minimum Swaps to Make Sequences Increasing
      • 14.23.1 Problem Metadata
      • 14.23.2 Description
      • 14.23.3 Examples
      • 14.23.4 Constraints
      • 14.23.5 Solution - DP with Keep/Swap States
    • 14.24 Count Square Submatrices with All Ones
      • 14.24.1 Problem Metadata
      • 14.24.2 Description
      • 14.24.3 Examples
      • 14.24.4 Constraints
      • 14.24.5 Solution - Dynamic Programming (In-Place)
    • 14.25 Custom Fibonacci Sequence
      • 14.25.1 Metadata
      • 14.25.2 Description
      • 14.25.3 Examples
      • 14.25.4 Constraints
      • 14.25.5 Walkthrough
      • 14.25.6 Analysis
      • 14.25.7 Implementation Steps
      • 14.25.8 Java Code
    • 14.26 Find Index Combinations with Target Weight Sum
      • 14.26.1 Problem Metadata
      • 14.26.2 Description
      • 14.26.3 Examples
      • 14.26.4 Input Format
      • 14.26.5 Output Format
      • 14.26.6 Constraints
      • 14.26.7 Solution - Backtracking with Unbounded Knapsack
    • 14.27 Generate Valid Angle Bracket Sequences
      • 14.27.1 Problem Metadata
      • 14.27.2 Description
      • 14.27.3 Examples
      • 14.27.4 Constraints
      • 14.27.5 Solution - Backtracking
    • 14.28 Minimum Plans to Reach Target Bandwidth
      • 14.28.1 Problem Metadata
      • 14.28.2 Description
      • 14.28.3 Examples
      • 14.28.4 Input Format
      • 14.28.5 Output Format
      • 14.28.6 Constraints
      • 14.28.7 Solution - Dynamic Programming (Unbounded Knapsack)
    • 14.29 Fibonacci Sequence
      • 14.29.1 Problem Metadata
      • 14.29.2 Description
      • 14.29.3 Examples
      • 14.29.4 Constraints
      • 14.29.5 Solution 1 - Top-Down Recursion (Naïve)
      • 14.29.6 Solution 2 - Tail Recursion
      • 14.29.7 Solution 3 - Bottom-Up DP
      • 14.29.8 Solution 4 - Memoized Recursion
    • 14.30 Longest Common Substring
      • 14.30.1 Problem Metadata
      • 14.30.2 Description
      • 14.30.3 Examples
      • 14.30.4 Constraints
      • 14.30.5 Solution - 2D DP Table
    • 14.31 Minimum Operations to One
      • 14.31.1 Problem Metadata
      • 14.31.2 Description
      • 14.31.3 Examples
      • 14.31.4 Constraints
      • 14.31.5 Solution - Memoized Recursion
    • 14.32 Retrieve Optimal Computation
      • 14.32.1 Problem Metadata
      • 14.32.2 Description
      • 14.32.3 Examples
      • 14.32.4 Constraints
      • 14.32.5 Solution - Memoized Recursion
  • 15 Problem Index
    • 15.1 Quick Lookup by Problem ID
      • 15.1.1 LeetCode
      • 15.1.2 HackerRank
      • 15.1.3 Firecode
      • 15.1.4 Other
      • 15.1.5 Interview
    • 15.2 Lookup by Difficulty
      • 15.2.1 Easy Problems
      • 15.2.2 Medium Problems
      • 15.2.3 Hard Problems
      • 15.2.4 Unclassified Problems
    • 15.3 Lookup by Chapter
    • 15.4 Lookup by Technique
      • 15.4.1 Dynamic Programming
      • 15.4.2 Backtracking
      • 15.4.3 Two Pointer
      • 15.4.4 Sliding Window
      • 15.4.5 Greedy
      • 15.4.6 Data Structures
      • 15.4.7 Searching
      • 15.4.8 Matrix Traversal
      • 15.4.9 Tree Traversal
      • 15.4.10 Graph Traversal
      • 15.4.11 Sorting
      • 15.4.12 Statistical Sampling
      • 15.4.13 Game Theory & Optimization
    • 15.5 Lookup by Study Plan
      • 15.5.1 Blind 75
      • 15.5.2 NeetCode 150
      • 15.5.3 Grind 75
    • 15.6 Lookup by Company
    • 15.7 How to Update This Index
      • 15.7.1 Example CSV Entry
      • 15.7.2 Tags Format
      • 15.7.3 Techniques Format
    • 15.8 Summary Statistics
  • References
  • Published with bookdown

Technical Interview

14.10 Word Search

14.10.1 Problem Metadata

  • Platform: LeetCode
  • Problem ID: 79
  • Difficulty: Medium
  • URL: https://leetcode.com/problems/lc-word-search/
  • Tags: Grind 75, Blind 75, NeetCode 150
  • Techniques: Backtracking: Mark-Unmark, Depth First Search, Matrix, Backtracking

14.10.2 Description

Given a board of characters and a target word, check if the word can be constructed from sequentially adjacent cells (horizontal or vertical). Each cell can be used at most once.

14.10.3 Examples

Input:
[["A","B","C","E"],
 ["S","F","C","S"],
 ["A","D","E","E"]], word = "ABCCED"
Output: true

14.10.4 Constraints

  • 1 <= board.length, board[0].length <= 6
  • 1 <= word.length <= 15

14.10.5 Solution - DFS Backtracking

14.10.5.1 Walkthrough

Try every cell as a starting point by iterating through the entire board. For each starting position, create a fresh visited array to track which cells are used in the current path. Use DFS to recursively match characters by exploring all four directions (up, down, left, right).

Key Backtracking Steps: 1. Mark: Mark the current cell as visited before exploring neighbors 2. Explore: Recursively try all four directions with the next character 3. Unmark: Restore the cell to unvisited before returning (critical for backtracking!)

This mark-unmark pattern ensures cells can be reused when trying different starting positions or different paths. If we successfully match all characters (reach the end of the word), return true. Otherwise, backtrack and try other paths.

14.10.5.2 Analysis

  • Time Complexity: O(m × n × 4^L) where L is the word length. For each of m × n starting positions, DFS explores up to 4^L paths (4 directions at each of L steps, though pruned by visited checks)
  • Space Complexity: O(m × n + L) where O(m × n) is for the visited array and O(L) is the recursion stack depth

14.10.5.3 Implementation Steps

  1. Loop over all cells (i, j) in the board as potential starting positions
  2. For each starting position, create a fresh visited[m][n] boolean array
  3. Call dfs(board, visited, i, j, word, 0) to attempt matching from that position
  4. DFS Base Cases:
    • If index >= word.length(), return true (found complete word)
    • If out of bounds, return false
    • If already visited OR character doesn’t match, return false
  5. DFS Recursive Case:
    • Mark visited[i][j] = true
    • Recursively explore all 4 directions with index + 1
    • Backtrack: Set visited[i][j] = false before returning
  6. Return true if any starting position succeeds

14.10.5.4 Code - Java

public boolean exist(char[][] board, String word) {
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            boolean[][] visited = new boolean[board.length][board[0].length];

            boolean result = dfs(board, visited, i, j, word, 0);

            if (result) {
                return true;
            }
        }
    }

    return false;
}

private boolean dfs(char[][] board, boolean[][] visited, int i, int j, String word, int index) {
    // End of word reached - success
    if (index >= word.length()) {
        return true;
    }

    // Out of bounds check
    if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) {
        return false;
    }

    // Already visited OR character doesn't match
    if (visited[i][j] || word.charAt(index) != board[i][j]) {
        return false;
    }

    // Mark current cell as visited
    visited[i][j] = true;

    // Explore all four directions
    boolean found =
        dfs(board, visited, i + 1, j, word, index + 1) || // down
        dfs(board, visited, i - 1, j, word, index + 1) || // up
        dfs(board, visited, i, j + 1, word, index + 1) || // right
        dfs(board, visited, i, j - 1, word, index + 1);   // left

    // BACKTRACK: Unmark visited cell before returning
    visited[i][j] = false;

    return found;
}