Explanations, pseudocode, an embedded interactive visualization, implementations in JavaScript, Python, Java and Go, complexity and interview variations.
Checks every element from left to right until the target is found or the array ends. Works on unsorted data.
Searches a sorted array by repeatedly comparing the target with the middle element and discarding the half that cannot contain it.
First index whose value is ≥ target in a sorted array (binary-search variant).
First index whose value is > target in a sorted array.
Binary search that first decides which half is still sorted.
Repeatedly compares adjacent elements and swaps them when they are out of order, so the largest unsorted value bubbles to the end after every pass.
Divides the array into a sorted prefix and an unsorted suffix. Each pass selects the minimum of the unsorted part and swaps it into the next position of the prefix.
Builds a sorted prefix one element at a time: the next element (the key) is lifted out, larger elements of the prefix shift one step right, and the key drops into the gap.
Divide the array in halves, sort each recursively, merge the sorted halves.
Partition around a pivot, then sort both sides recursively.
Build a max-heap, then repeatedly move the maximum to the end.
Count occurrences of each key in a small integer range.
Stable sort digit by digit, least significant first.
Reverses an array in place with two pointers: swap the outermost pair, move both pointers inward, and stop when they meet.
Finds two indexes whose values add up to a target in one pass, storing each value's index in a hash map and looking up the complement (target − value).
Inserts a new node at a given position: walk to the node just before the position, point the new node at its successor, then re-link the predecessor to the new node.
A stack is Last-In-First-Out (LIFO): push adds to the top, pop removes from the top. A fixed capacity shows overflow (push when full) and underflow (pop when empty).
A queue is First-In-First-Out (FIFO): enqueue adds at the rear, dequeue removes from the front. This circular-array queue wraps the rear pointer around to reuse freed slots.
Base case + recursive case; how the call stack grows and unwinds.
n! = n × (n−1)!, the classic first recursive function.
Naive recursion recomputes subproblems — exponential time.
Move n−1 disks aside, move the largest, move n−1 back on top.
Include / exclude each element to generate the power set.
Choose, recurse, un-choose to list every ordering.
Place queens row by row, backtracking on conflicts.
Fill empty cells with valid digits, undo on dead ends.
Recursively visits the left subtree, then the node itself, then the right subtree (Left → Root → Right). On a Binary Search Tree this produces the values in sorted order.
Root → Left → Right; used to copy/serialise trees.
Left → Right → Root; used to delete/evaluate trees.
BFS on a tree with a queue, level by level.
1 + max(height(left), height(right)).
Deepest node that has both targets in its subtree.
Explores the graph level by level using a queue. Because vertices are discovered in order of distance, BFS also finds the shortest path (fewest edges) from the start in an unweighted graph.
Goes as deep as possible along each branch before backtracking. This recursive version uses the call stack as its implicit stack.
Shortest paths with non-negative weights using a min-priority queue.
Relax every edge V−1 times; detects negative cycles.
All-pairs shortest paths by dynamic programming over intermediate vertices.
Linear order of a DAG respecting every edge (Kahn's algorithm / DFS).
Minimum spanning tree by adding cheapest edges with Union-Find.
Grow a minimum spanning tree from one vertex with a priority queue.
Pick the activity that finishes first, repeat.
Take items by best value/weight ratio, splitting the last one.
Schedule highest-profit jobs into the latest free slot before their deadline.
Merge the two least frequent symbols repeatedly to build an optimal prefix code.
Memoise or tabulate to turn O(2ⁿ) into O(n).
ways(n) = ways(n−1) + ways(n−2).
Best value with capacity W, each item taken at most once.
2-D table over prefixes of both strings.
O(n²) DP or O(n log n) with patience sorting.
Fewest coins (or number of ways) to make an amount.
Best parenthesisation via interval DP.
paths(r, c) = paths(r−1, c) + paths(r, c−1).