3Sum — Sort Then Two-Pointer Search
Sort the array, fix one value, and sweep the remaining window with two pointers while skipping duplicates.
Short articles that give you the key insights and takeaways from popular coding problems.
Sort the array, fix one value, and sweep the remaining window with two pointers while skipping duplicates.
Compare adjacent words to derive ordering constraints between letters, then perform Kahn’s algorithm.
Capture the lowest buying price so far and measure profit deltas in a single scan.
Use a queue to traverse nodes level by level, collecting values into lists per depth.
Compute the best downward gain from each node while tracking the maximum path that can pass through the node.
Reuse the recurrence f(n) = f(n-1) + f(n-2) with constant space to count distinct stair-climbing ways.
Build the minimum coins needed for each amount up to target using previously computed states.
Perform depth-first search, deciding how many times to pick each candidate while keeping the combination in non-decreasing order.
Hold both ends of the array and always move the shorter wall inward to maximise trapped area in one pass.
Track seen numbers in a set; if a value appears twice, return true immediately.
Use the recurrence dp[i] = dp[i >> 1] + (i & 1) to build popcounts from 0 to n.
Use Kahn’s algorithm (BFS) or DFS to detect cycles in the prerequisite graph and decide feasibility.
Use dp[i] representing ways to decode the prefix up to i by considering one or two-digit translations.
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.
Treat the graph as a disjoint set; every edge must connect two different components and the graph needs exactly n − 1 edges.
Use a canonical key (sorted string or frequency vector) to bucket anagrams together in linearithmic time overall.
At each house decide whether to rob it by comparing robbing plus skipping previous versus skipping now.
Build a node structure with child maps and an isWord flag to support insert, search, and prefix queries in O(L).
Walk the sorted intervals, append those ending before the new interval, merge overlaps, then append the rest.
Swap left and right children at every node using recursion or BFS to mirror the tree.
Track the furthest index reachable so far; if you ever fall behind the current index, the end cannot be reached.
Run two pointers at different speeds; if a cycle exists they eventually collide without any extra memory.
Treat every character and gap as a center, expanding while the ends mirror each other to find the best span.
Slide a window while remembering the last index of each character so duplicates never stay in view.
Leverage BST ordering: move left or right until the current node splits the two targets.
Return 1 plus the maximum depth of left and right subtrees using recursion.
Maintain both the highest and lowest running product because negative numbers can flip the sign of the result.
Carry a running best prefix; reset when it becomes negative so the subarray that follows starts fresh.
Sort intervals by start time and ensure each meeting starts after the previous one ends.
Separate start and end times, sort both, and sweep to find the maximum number of concurrent meetings.
Sort intervals by start time and greedily merge overlapping ranges with a running tail pointer.
Push the head of each list into a min-heap and repeatedly extract the smallest node to build the merged list.
Use a dummy head and advance two pointers, always stitching the smaller node into the merged list.
Expand the window to satisfy the requirement, then shrink from the left to find the smallest valid window.
Compute the expected sum of 0..n and subtract the observed sum to recover the missing value in constant space.
Clear the lowest set bit repeatedly by applying n & (n - 1) until the number becomes zero.
Initialise each node in its own set and union edges; the remaining number of roots equals the connected components.
Treat the grid as a graph; whenever you encounter land, run DFS/BFS to sink the entire island and increment the count.
Expand around every index and gap to count palindromic substrings without building a DP table.
Compute prefix products and suffix products to fill the output without division.
Advance a front pointer n steps ahead so that when it hits null, the trailing pointer sits right before the node to remove.
Break the list in half, reverse the back half, and weave the two lists together to match L0, Ln, L1, Ln-1 ... order.
Swap symmetric bit pairs by shifting and masking until you have reversed the 32-bit integer.
Walk the list once while rewiring next pointers to build the reversed list in-place.
Transpose the square matrix then reverse each row to achieve an in-place 90° rotation.
Traverse both trees simultaneously; if nodes mismatch in structure or value, return false.
Decide which side of the pivot is sorted at every step so the binary search converges on the target in logarithmic time.
Use the first row and column as markers to remember which rows and columns should be zeroed without extra space.
Maintain four boundaries (top, bottom, left, right) and peel layers while updating them carefully.
Either serialise both trees and search for the subtree pattern or perform depth-first node comparisons with early pruning.
Use XOR for partial sums and AND/shift for carries until no carry remains.
Count element frequencies with a hash map and extract the top k using either a min-heap or frequency buckets.
Eliminate the quadratic brute force by caching complements as you sweep the array once.
Count paths from top-left to bottom-right by summing ways from the top and left neighbors.
Count character frequencies in both strings and ensure the counts match exactly.
Move pointers inward while skipping non-alphanumeric characters; compare lowercased characters for equality.
Push opening brackets onto a stack and ensure every closing bracket matches the most recent opener.
Treat indices as graph nodes and BFS through valid dictionary cuts to decide whether the entire string can be segmented.
Perform DFS from each cell, marking visited cells temporarily to avoid reuse within the same path.
Insert all words into a trie and prune DFS paths that are not prefixes, collecting matches as you traverse.
Master the backtracking pattern by learning when to explore paths, how to prune invalid branches, and why state management is critical.
Internalise the binary search loop so you can apply it to sorted arrays, answer-range problems, and monotonic predicates.
Learn how BFS explores graphs layer by layer, powers shortest unweighted paths, and pairs perfectly with queues.
Construct a prefix tree that supports insert and search in O(L), and learn how to extend it for autocomplete prompts.
Understand DFS as a flexible template for exploring as far as possible, powering cycle detection, connected components, and backtracking.
Move beyond unweighted graphs by mastering Dijkstra’s priority-queue based approach to shortest paths with non-negative weights.
Translate interviews into graph problems by choosing the right representation: adjacency lists, matrices, edge lists, and implicit neighbors.
Learn how heaps provide O(log n) insertions and removals for extreme values, powering top-K, scheduling, and streaming problems.
Turn the Interview Flowchart into a repeatable script: clarify, pattern match, and choose the right tool without freezing mid-round.
Use Kadane’s dynamic programming insight to find maximum subarray sums, track boundaries, and pivot to 2D variations.
Demystify singly and doubly linked lists, pointer manipulation, and fast/slow patterns that dominate interview questions.
Learn how to recognise hash map opportunities, pick collision strategies, and explain amortised behaviour with confidence.
Master core string search techniques: naive sliding window, Rabin-Karp hashing, and prefix-function-based KMP.
Learn how queues model real-world workflows, feed BFS, and support monotonic optimisations like sliding window maximum.
Compare sorting algorithms, understand when to reach for mergesort vs. quicksort, and explain stability, in-place trade-offs, and custom comparators.
Learn how to order tasks with prerequisites using Kahn’s algorithm or DFS post-order, and recognise when topological sort applies.