Category

algorithms

23 articles across 8 sub-topics

Two Pointer Technique: Solving Pair and Partition Problems in O(n)

Two Pointer Technique: Solving Pair and Partition Problems in O(n)

TLDR: Place one pointer at the start and one at the end of a sorted array. Move them toward each other based on a comparison condition. Every classic pair/partition problem that naively runs in O(n²)

19 min read
Tries (Prefix Trees): The Data Structure Behind Autocomplete

Tries (Prefix Trees): The Data Structure Behind Autocomplete

TLDR: A Trie stores strings character by character in a tree, so every string sharing a common prefix shares those nodes. Insert and search are O(L) where L is the word length. Tries beat HashMaps on

20 min read
Sliding Window Technique: From O(n·k) Scans to O(n) in One Pass

Sliding Window Technique: From O(n·k) Scans to O(n) in One Pass

TLDR: Instead of recomputing a subarray aggregate from scratch on every shift, maintain it incrementally — add the incoming element, remove the outgoing element. For a fixed window this costs O(1) per

20 min read
Merge Intervals Pattern: Solve Scheduling Problems with Sort and Sweep

Merge Intervals Pattern: Solve Scheduling Problems with Sort and Sweep

TLDR: Sort intervals by start time, then sweep left-to-right and merge any interval whose start ≤ the current running end. O(n log n) time, O(n) space. One pattern — three interview problems solved.

16 min read

In-Place Reversal of a Linked List: The 3-Pointer Dance Every Interviewer Expects

TLDR: Reversing a linked list in O(1) space requires three pointers — prev, curr, and next. Each step: save next, flip curr.next to point backward, advance both prev and curr. Learn this once and you unlock four reversal variants that appear constant...

17 min read
Fast and Slow Pointer: Floyd's Cycle Detection Algorithm Explained

Fast and Slow Pointer: Floyd's Cycle Detection Algorithm Explained

TLDR: Move a slow pointer one step and a fast pointer two steps through a linked structure. If they ever meet, a cycle exists. Then reset one pointer to the head and advance both one step at a time —

21 min read

DFS — Depth-First Search: Go Deep Before Going Wide

TLDR: DFS explores a graph by diving as deep as possible along each path before backtracking, using a call stack (recursion) or an explicit stack. It is the go-to algorithm for cycle detection, path finding, topological sort, and connected components...

16 min read

Cyclic Sort: Find Missing and Duplicate Numbers in O(n) Time, O(1) Space

TLDR: If an array holds n numbers in range [1, n], each number belongs at index num - 1. Cyclic sort places every element at its correct index in O(n) time using O(1) space — then a single scan reveals every missing and duplicate number. Five intervi...

16 min read

BFS — Breadth-First Search: Level-by-Level Graph Exploration

TLDR: BFS explores a graph level by level using a FIFO queue, guaranteeing the shortest path in unweighted graphs. Recognize BFS problems by keywords: "shortest path," "minimum steps," or "level order." Time: O(V + E). Space: O(V). Mark nodes visited...

17 min read

Two Heaps Pattern: Find the Median of a Data Stream Without Sorting

TLDR: Two Heaps partitions a stream into two sorted halves. A max-heap holds everything below the median; a min-heap holds everything above it. Keep the heaps size-balanced and you can read the median from either top in O(1) — no sorting needed, ever...

16 min read

Top K Elements Pattern: Find the Best K Without Sorting Everything

TLDR: To find the top K largest elements, maintain a min-heap of size K. For every new element, push it onto the heap. If the heap exceeds K, evict the minimum. After processing all N elements, the heap holds exactly the K largest. O(N log K) time — ...

16 min read

K-Way Merge Pattern: Merge K Sorted Sequences with a Min-Heap

TLDR: K-Way Merge uses a min-heap with exactly one entry per sorted input list. Each entry stores the current element's value plus the coordinates to find the next element in that list. Pop the minimum (global smallest), append it to output, push the...

17 min read