Practice Lab

Problem Solving Patterns

PatternSignalTypical ComplexitySolved ProblemsNotes
Two pointersSorted array, pair search, partitioningO(n) after sorting or per fixed index-Watch pointer movement invariants and duplicate skipping.
Sort + fixed index + two pointersUnique triplets, 3Sum-style pair search after choosing one valueO(n^2)3SumSort, skip duplicate fixed values, then move inward based on sum.
Sliding windowContiguous subarray or substringO(n)-Track what makes a window valid.
Hash map complement lookupPair sum or "find prior value that completes current value"O(n)Two SumCheck for the complement before inserting the current value to avoid reusing the same index.
Hash map countingFrequency, lookup, deduplicationO(n)-Clarify collision and memory trade-offs if asked.
Stack matchingParentheses, nested structures, last-open-first-close validationO(n)Valid ParenthesesPush opening items, match closing items against the latest opening item.
Stack cancellationAdjacent duplicate removal, collision-style cancellation, cleaned prefix trackingO(n)Remove All Adjacent Duplicates In StringThe stack top is the latest kept item; pop it when the current item cancels it.
Monotonic stackNext greater or next smaller value, span, waiting timeO(n)Daily TemperaturesStore unresolved indices; pop when the current value resolves the top.
Sort intervals + scanMerge intervals, meeting rooms, overlap detectionO(n log n)Merge IntervalsSort by start, then compare each interval with the last merged interval.
Track minimum so farSingle buy/sell profit, best difference with ordering constraintO(n)Best Time to Buy and Sell StockKeep the best prior buy price, then compute the profit if selling at the current value.
Greedy positive differencesUnlimited buy/sell, accumulate all local gainsO(n)Best Time to Buy and Sell Stock IIIf every positive edge can be captured independently, summing local gains equals the best global profit.
Greedy farthest reachJump Game, reachability over arrays, minimum jumps variantsO(n)Jump GameTrack the farthest index reachable so far; an index beyond that boundary is unreachable.
Recursive tree comparisonBinary tree equality, symmetry, subtree checksO(n)Same TreeHandle null cases first, then compare values and recurse into matching child branches.
DFS path buildingRoot-to-leaf paths, traversal with accumulated stateO(n)Binary Tree PathsCarry the path or state through recursion, then save it when the target condition is reached.
Prefix sum balanceLeft/right balance, running totals, pivot checksO(n)Find Pivot IndexKeep a running left sum and derive the right side from the total.
Prefix sum countsCount subarrays with a target sum, especially with negatives or zeroesO(n)Subarray Sum Equals KCount earlier prefix sums that equal currentPrefix - k; initialize 0 -> 1.
BFSShortest path in unweighted graphO(V + E)-Use queue and visited set.
DFSTraversal, components, recursionO(V + E)-Watch recursion depth.
Dynamic programmingOverlapping subproblemsVaries-Define state, transition, base cases.