Blind 75
Detailed walkthroughs for the canonical Blind 75 problems, focusing on the decision-making that leads to optimal patterns.
Two Sum — One-Pass Hash Map
Eliminate the quadratic brute force by caching complements as you sweep the array once.
Best Time to Buy and Sell Stock — Track a Running Minimum
Capture the lowest buying price so far and measure profit deltas in a single scan.
Longest Substring Without Repeating Characters — Sliding Window Map
Slide a window while remembering the last index of each character so duplicates never stay in view.
Longest Palindromic Substring — Expand Around Center
Treat every character and gap as a center, expanding while the ends mirror each other to find the best span.
Graph Valid Tree — Union Find Cycle Check
Treat the graph as a disjoint set; every edge must connect two different components and the graph needs exactly n − 1 edges.
Palindromic Substrings — Count by Expanding Centers
Expand around every index and gap to count palindromic substrings without building a DP table.
Container With Most Water — Two-Pointer Contraction
Hold both ends of the array and always move the shorter wall inward to maximise trapped area in one pass.
Word Break — BFS Over Split Positions
Treat indices as graph nodes and BFS through valid dictionary cuts to decide whether the entire string can be segmented.
Linked List Cycle — Floyd’s Tortoise and Hare
Run two pointers at different speeds; if a cycle exists they eventually collide without any extra memory.
Missing Number — Arithmetic Series Difference
Compute the expected sum of 0..n and subtract the observed sum to recover the missing value in constant space.
3Sum — Sort Then Two-Pointer Search
Sort the array, fix one value, and sweep the remaining window with two pointers while skipping duplicates.
Reorder List — Split, Reverse, Merge
Break the list in half, reverse the back half, and weave the two lists together to match L0, Ln, L1, Ln-1 ... order.
Alien Dictionary — Topological Sort on Characters
Compare adjacent words to derive ordering constraints between letters, then perform Kahn’s algorithm.
Remove Nth Node From End — One-Pass Two Pointers
Advance a front pointer n steps ahead so that when it hits null, the trailing pointer sits right before the node to remove.
Valid Parentheses — Stack-Based Matching
Push opening brackets onto a stack and ensure every closing bracket matches the most recent opener.
Merge Two Sorted Lists — Dummy Head Merge
Use a dummy head and advance two pointers, always stitching the smaller node into the merged list.
Merge k Sorted Lists — Min-Heap Merge
Push the head of each list into a min-heap and repeatedly extract the smallest node to build the merged list.
Maximum Product Subarray — Track Hi/Lo Products
Maintain both the highest and lowest running product because negative numbers can flip the sign of the result.
Find Minimum in Rotated Sorted Array — Binary Search on Sorted Halves
Use binary search to locate the pivot: if the mid element is greater than the rightmost element, search the right half; otherwise search the left.
Search in Rotated Sorted Array — Pivot-Aware Binary Search
Decide which side of the pivot is sorted at every step so the binary search converges on the target in logarithmic time.
Combination Sum — Backtracking with Reuse
Perform depth-first search, deciding how many times to pick each candidate while keeping the combination in non-decreasing order.
Rotate Image — Layered Matrix Transpose and Reverse
Transpose the square matrix then reverse each row to achieve an in-place 90° rotation.
Group Anagrams — Canonical Signature Hashing
Use a canonical key (sorted string or frequency vector) to bucket anagrams together in linearithmic time overall.
Maximum Subarray — Kadane’s Running Sum
Carry a running best prefix; reset when it becomes negative so the subarray that follows starts fresh.
Spiral Matrix — Layer-by-Layer Traversal
Maintain four boundaries (top, bottom, left, right) and peel layers while updating them carefully.
Jump Game — Greedy Furthest Reach
Track the furthest index reachable so far; if you ever fall behind the current index, the end cannot be reached.
Merge Intervals — Sort Then Sweep
Sort intervals by start time and greedily merge overlapping ranges with a running tail pointer.
Insert Interval — Merge While Inserting
Walk the sorted intervals, append those ending before the new interval, merge overlaps, then append the rest.
Subtree of Another Tree — Serialize or DFS Compare
Either serialise both trees and search for the subtree pattern or perform depth-first node comparisons with early pruning.
Unique Paths — Grid Dynamic Programming
Count paths from top-left to bottom-right by summing ways from the top and left neighbors.
Reverse Bits — Bitwise Swapping
Swap symmetric bit pairs by shifting and masking until you have reversed the 32-bit integer.
Number of 1 Bits — Brian Kernighan’s Trick
Clear the lowest set bit repeatedly by applying n & (n - 1) until the number becomes zero.
Coin Change — Bottom-Up Dynamic Programming
Build the minimum coins needed for each amount up to target using previously computed states.
Number of Connected Components — Union-Find Tracking
Initialise each node in its own set and union edges; the remaining number of roots equals the connected components.
Climbing Stairs — Fibonacci DP
Reuse the recurrence f(n) = f(n-1) + f(n-2) with constant space to count distinct stair-climbing ways.
House Robber — Linear DP with Two Choices
At each house decide whether to rob it by comparing robbing plus skipping previous versus skipping now.
Number of Islands — DFS Flood Fill
Treat the grid as a graph; whenever you encounter land, run DFS/BFS to sink the entire island and increment the count.
Set Matrix Zeroes — Mark First Row and Column
Use the first row and column as markers to remember which rows and columns should be zeroed without extra space.
Minimum Window Substring — Sliding Window with Counts
Expand the window to satisfy the requirement, then shrink from the left to find the smallest valid window.
Reverse Linked List — Iterative Pointer Reversal
Walk the list once while rewiring next pointers to build the reversed list in-place.
Word Search — Backtracking on a Grid
Perform DFS from each cell, marking visited cells temporarily to avoid reuse within the same path.
Course Schedule — Topological Sort with Cycle Detection
Use Kahn’s algorithm (BFS) or DFS to detect cycles in the prerequisite graph and decide feasibility.
Implement Trie — Prefix Tree Operations
Build a node structure with child maps and an isWord flag to support insert, search, and prefix queries in O(L).
Counting Bits — DP from Lowest Set Bit
Use the recurrence dp[i] = dp[i >> 1] + (i & 1) to build popcounts from 0 to n.
Word Search II — Trie-Guided Backtracking
Insert all words into a trie and prune DFS paths that are not prefixes, collecting matches as you traverse.
Contains Duplicate — Hash Set Membership
Track seen numbers in a set; if a value appears twice, return true immediately.
Decode Ways — DP Over Digits
Use dp[i] representing ways to decode the prefix up to i by considering one or two-digit translations.
Top K Frequent Elements — Bucket or Heap
Count element frequencies with a hash map and extract the top k using either a min-heap or frequency buckets.
Meeting Rooms II — Sweep Line on Start/End Times
Separate start and end times, sort both, and sweep to find the maximum number of concurrent meetings.
Invert Binary Tree — Recursive Swap
Swap left and right children at every node using recursion or BFS to mirror the tree.
Same Tree — Parallel DFS Comparison
Traverse both trees simultaneously; if nodes mismatch in structure or value, return false.
Binary Tree Level Order Traversal — BFS by Levels
Use a queue to traverse nodes level by level, collecting values into lists per depth.
Maximum Depth of Binary Tree — DFS Height Calculation
Return 1 plus the maximum depth of left and right subtrees using recursion.
Lowest Common Ancestor in BST — Guided Descent
Leverage BST ordering: move left or right until the current node splits the two targets.
Product of Array Except Self — Prefix and Suffix Multiplication
Compute prefix products and suffix products to fill the output without division.
Valid Anagram — Frequency Comparison
Count character frequencies in both strings and ensure the counts match exactly.
Sum of Two Integers — Bitwise Addition
Use XOR for partial sums and AND/shift for carries until no carry remains.
Meeting Rooms — Sort and Check Overlaps
Sort intervals by start time and ensure each meeting starts after the previous one ends.
Binary Tree Maximum Path Sum — DFS with Returning Branch Gains
Compute the best downward gain from each node while tracking the maximum path that can pass through the node.
Valid Palindrome — Two-Pointer Scan Ignoring Non-Alphanumerics
Move pointers inward while skipping non-alphanumeric characters; compare lowercased characters for equality.