Dinesh/ Blog

Article series · 66 lessons

Algorithms & data structures

Learn to recognize a problem’s structure and explain why your solution works.

Read the series introduction →

Start this series

Follow the sequence below, or pick up where you left off. Nothing is locked.

What you’ll work toward & your progress

What you’ll work toward

  • Practice an algorithm without confusing recall with proof
  • Choose a data structure from its contract
  • Record a concrete gap and a regression test
Loading this browser’s progress…

Completion is stored on this device only. Nothing is locked; start where it makes sense.

Series preparation

Explicitly listed by the series introduction; lesson order does not create a prerequisite.

Before the first lesson

These are the starting lesson’s prerequisites, not requirements for every advanced topic below.

How to practise this subject

For each family, state the input contract, trace a small example, produce a counterexample to a tempting shortcut, and compare your implementation with a bounded brute-force oracle.

Lessons in order

  1. 01beginner · 19 min

    The LeetCode Game — How to Read a Problem

    Read input, output and value bounds together; derive work and storage costs, then justify algorithm preconditions rather than guessing a universal time limit.

  2. 02beginner · 16 min

    Big-O in Practice — Counting, Not Vibes

    How to derive the cost of a Python data structure operation from how it is built, instead of memorising a table you half-remember under pressure.

  3. 03beginner · 15 min

    Python for Speed — The 20 Idioms That Matter

    The small set of standard-library tools that turn a fifteen-line design review solution into a five-line one, and the syntax you must be able to type without thinking.

  4. 04beginner · 17 min

    Arrays & Strings — In-Place Discipline

    Derive read/write-pointer compaction and partitioning from loop invariants, then test the boundary cases that make in-place mutation safe.

  5. 05beginner · 16 min

    Hashing — Your Default Weapon

    The complement-lookup skeleton and the general move of trading memory for time, which is the first thing to try whenever a problem looks quadratic.

  6. 06beginner · 18 min

    UMPIRE — The Problem-Solving Framework

    A six-step practice checklist for clarifying contracts, comparing approaches, planning, implementing, testing and analyzing costs—without a fixed completion-time promise.

  7. 07intermediate · 24 min

    Two Pointers I — Opposite Ends

    Two indices converging from the ends of a sorted array, and the monotonicity argument that proves you can discard a whole row of candidates with one comparison.

  8. 08intermediate · 18 min

    Two Pointers II — Same Direction & Fast-Slow

    Two pointers moving the same way at different speeds: the read/write compaction you already know, and Floyd's cycle detection, which finds a loop in O(1) memory.

  9. 09intermediate · 14 min

    Sliding Window I — Fixed Size

    Maintaining a window aggregate incrementally so that sliding costs O(1) instead of O(k), which turns the obvious O(n*k) scan into a single pass.

  10. 10intermediate · 15 min

    Sliding Window II — Variable Size

    Expand right unconditionally, shrink left while the invariant is broken, record at the right moment — and the amortisation argument that keeps it linear.

  11. 11intermediate · 17 min

    Sliding Window III — With Counts

    Windows whose validity depends on a frequency map, and the single integer counter that lets you test that condition in O(1) instead of rescanning the map.

  12. 12intermediate · 16 min

    Prefix Sums & Difference Arrays

    Precomputing cumulative sums to answer range queries in O(1), and the prefix-plus-hashmap combination that counts subarrays where sliding windows are unsound.

  13. 13intermediate · 16 min

    Binary Search I — The Template

    One binary search template with a half-open invariant that never loops forever and never goes off by one, and the discipline of owning exactly one version of it.

  14. 14intermediate · 17 min

    Binary Search II — Rotated & 2D

    Binary search when the array is no longer sorted end-to-end: decide which half is still sorted, then discard the other. Same trick flattens a 2D matrix into one index space.

  15. 15intermediate · 17 min

    Binary Search III — On the Answer

    Stop searching the array and start searching the answer range: define a monotonic feasibility predicate, then binary search the smallest x for which it holds.

  16. 16intermediate · 19 min

    Stacks — LIFO & Matching

    Nesting, matching, undo and expression evaluation are one idea: push what you expect, pop when it arrives, and carry auxiliary state alongside for O(1) queries like min.

  17. 17intermediate · 23 min

    Monotonic Stack

    Next greater and next smaller element in O(n): keep a stack whose order never breaks, and pop everything the current element resolves.

  18. 18intermediate · 25 min

    Queues & Monotonic Deques

    The queue as BFS substrate, and the monotonic deque that answers sliding-window maximum in O(n) by discarding candidates that can never win again.

  19. 19intermediate · 20 min

    Linked Lists I — Traversal & Reversal

    The dummy head that deletes most linked-list edge cases, and the prev/cur/next three-step reversal you should be able to write without thinking.

  20. 20intermediate · 20 min

    Linked Lists II — Cycles, Middle, Reorder

    Floyd's tortoise and hare for cycle detection and entry point, finding the middle in one pass, and composing split-reverse-merge to reorder a list in O(1) space.

  21. 21intermediate · 18 min

    Recursion & the Call Stack

    Base case discipline, hand-tracing a call stack, and knowing when recursion is the wrong tool — the substrate under trees, backtracking and dynamic programming.

  22. 22intermediate · 19 min

    Trees I — DFS Traversals

    Preorder, inorder and postorder as one recursive skeleton with the work line moved, plus the iterative forms and when they are worth writing.

  23. 23intermediate · 19 min

    Trees II — BFS / Level Order

    Snapshot the queue length at the top of each iteration and you have level-order traversal — the one trick behind right side view, zigzag, and minimum depth.

  24. 24intermediate · 21 min

    Trees III — BST Properties

    Two views of a binary search tree: inorder traversal yields sorted order, and validation requires passing bounds down rather than comparing parent to child.

  25. 25advanced · 21 min

    Trees IV — Paths & Accumulators

    The return-one-thing-track-another pattern: a recursion whose return value serves the parent while a separate accumulator records the global best.

  26. 26intermediate · 20 min

    Heaps & Top-K

    When a problem asks for the K largest, K smallest, or K most frequent of anything, you almost never need to sort. A size-K heap answers it in O(n log k).

  27. 27intermediate · 23 min

    Graphs I — Representation & DFS

    Build the adjacency list, carry a visited set, and recognise that a grid is already a graph — then count connected components with a depth-first sweep.

  28. 28intermediate · 19 min

    Graphs II — BFS & Shortest Path

    Breadth-first search level by level, why it gives shortest paths in unweighted graphs, and the multi-source variant that solves nearest-X grid problems in one sweep.

  29. 29intermediate · 18 min

    Graphs III — Topological Sort

    Kahn's in-degree algorithm for ordering tasks with dependencies, and why a queue that empties early is exactly a cycle detector.

  30. 30intermediate · 18 min

    Graphs IV — Union-Find (DSU)

    Fifteen lines of disjoint-set union with path compression and union by rank, and the family of connectivity problems it collapses into near-constant time.

  31. 31intermediate · 19 min

    Backtracking I — Subsets & Combinations

    One choose-explore-unchoose template that generates subsets, combinations and permutations, plus the two ways to skip duplicates without producing duplicate output.

  32. 32intermediate · 21 min

    Backtracking II — Constraint Grids

    Prune partial assignments, maintain column and diagonal constraints, and restore search state in N-Queens, Word Search and Sudoku.

  33. 33intermediate · 25 min

    DP I — Recognising Overlapping Subproblems

    DP is recursion plus a cache. Write the recursion, add lru_cache, convert to a table — mechanically, in that order, every time.

  34. 34intermediate · 20 min

    DP II — 1D Linear

    Define dp[i] in English before you write code. The House Robber family, and why most DP bugs are undefined state rather than a wrong transition.

  35. 35intermediate · 21 min

    DP III — Take / Skip Decisions

    The binary-decision DP shape: at each index you either act or you don't. State machines for cooldown, reachability for Jump Game, and split points for Word Break.

  36. 36intermediate · 20 min

    DP IV — Subsequences (LIS / LCS)

    Longest Increasing Subsequence and Longest Common Subsequence are the two parent problems behind a huge family. Know both cold and most subsequence DP becomes recognition.

  37. 37intermediate · 17 min

    DP V — 0/1 Knapsack

    Derive the 0/1 knapsack recurrence, prove why compressed capacity updates descend, and adapt the state to partition, sign assignments and reconstruction.

  38. 38intermediate · 16 min

    DP VI — Unbounded Knapsack

    The knapsack where each item is available in unlimited supply. One character changes from 0/1 — the capacity loop direction — and Coin Change, Rod Cutting, and Combination Sum IV all fall out of it.

  39. 39intermediate · 19 min

    DP VII — 2D Grid

    Grid DP is the friendliest dynamic programming: each cell is built from the one above and the one to its left. Use it to make DP feel mechanical before string DP.

  40. 40intermediate · 21 min

    DP VIII — String DP / Edit Distance

    The two-string DP table: dp[i][j] over prefixes, the three-way insert/delete/replace recurrence, and how Distinct Subsequences and Interleaving String are the same table with different transitions.

  41. 41intermediate · 21 min

    DP IX — Palindromes

    Expand-around-centre versus the 2D palindrome table: two O(n^2) approaches with very different failure rates, and when partitioning forces you back to explicit DP.

  42. 42intermediate · 23 min

    DP X — Interval DP

    dp[i][j] over ranges with an inner split point k: why Burst Balloons only works when you think about the LAST balloon, and how sentinel padding removes the boundary cases.

  43. 43intermediate · 24 min

    DP XI — State Machine

    Naming your DP states explicitly: hold and free arrays for the stock problems, why drawing the transition diagram first collapses the difficulty, and how the k-transaction version generalises.

  44. 44intermediate · 20 min

    DP XII — Bitmask DP

    Encoding a subset as an integer: when n is at most 20 the exponential state is affordable, the standard submask and popcount idioms, and why the constraint tells you before the problem does.

  45. 45advanced · 20 min

    Tries — Prefix Trees

    Children dict plus terminal flag: building a trie in ten lines, and why Word Search II is trie-plus-backtracking rather than a dictionary scan.

  46. 46advanced · 23 min

    Bit Manipulation

    XOR cancels itself, n & (n-1) clears the lowest set bit, and how to add two integers without the plus operator. The handful of bit tricks that actually recur in practice.

  47. 47advanced · 26 min

    Math & Number Theory

    Fast exponentiation by squaring, the sieve of Eratosthenes, Euclid's GCD, and binary search on integers — the four math routines worth memorising outright.

  48. 48advanced · 23 min

    Intervals

    Sort by start, merge on overlap: the four-problem interval family, and why Meeting Rooms II needs a heap on top of the sort.

  49. 49advanced · 27 min

    Greedy — And Proving It Works

    Greedy feels right and is often wrong. The exchange argument, the counter-example hunt you should run first, and four problems where greedy genuinely is optimal.

  50. 50advanced · 19 min

    Matrix Manipulation

    Transpose then reverse to rotate in place, four-boundary control for spiral traversal, and using the first row and column as O(1) marker storage.

  51. 51advanced · 20 min

    Design I — Composing for O(1)

    Hashmap plus doubly-linked list for LRU, array plus index map for GetRandom, and the general move of combining two structures so each covers the other's weakness.

  52. 52advanced · 27 min

    Design II — Streams & Time

    Two balanced heaps for a running median, binary search over versioned timestamps, and merging k sorted feeds — the design patterns for data that arrives over time.

  53. 53advanced · 23 min

    Sorting & Custom Comparators

    Partition-based selection and comparator design: Dutch national flag, quickselect for Kth largest in O(n) average, and cmp_to_key for orderings that are not a simple key.

  54. 54advanced · 20 min

    Segment Trees & Fenwick (BIT)

    Range query with point update in O(log n): the recursive segment tree skeleton, the Fenwick tree's low-bit trick, and how to recognise the rare problem that actually needs one.

  55. 55advanced · 19 min

    String Algorithms

    KMP's failure function, rolling hashes, and the Z-function — the three ways to avoid re-comparing characters you have already matched.

  56. 56advanced · 19 min

    Advanced Graphs

    Dijkstra as BFS with a priority queue, Bellman-Ford for negative weights and bounded hops, and the two minimum spanning tree algorithms — with the cue for choosing between them.

  57. 57advanced · 24 min

    Mock Set 1 — Mixed Easy/Medium, 45 min

    The first cold pressure test: two unseen Easy and two unseen Medium in 45 minutes, no hints, timer visible — run as an experiment that produces data about your classifier.

  58. 58advanced · 24 min

    Mock Set 2 — Narrated & Recorded

    Two unseen Mediums with screen/audio recording and evidence-based playback review.

  59. 59advanced · 25 min

    Blind 75 Sweep — Gap Audit

    Attempt-classify all 75 problems in one structured sweep, mark each cold, warm, hint or failed, and produce the personalised weak list that drives the rest of the track.

  60. 60advanced · 24 min

    Weak-Pattern Intensive

    Take the bottom three patterns from the L59 audit and repair them with the rebuild loop: blind template, three costumes, interleaved re-test.

  61. 61advanced · 25 min

    Mock Set 3 — One Hard, 45 min

    A timed unfamiliar-hard session: investigate decompositions, state invariants, and record partial progress and assistance.

  62. 62advanced · 21 min

    Company-Tagged — Microsoft & Amazon

    Two transferable coding mocks: extend a pair-sum problem and reason through a grid traversal, then assess your explanations and failure cases without claiming current employer question-frequency data.

  63. 63advanced · 18 min

    Senior-Flavour Coding

    Coding and design exercises for LRU caches, hit counters, logging limits, queues, and ring-based key routing.

  64. 64advanced · 24 min

    Contest Simulation

    A timed contest simulation with explicit triage, scoring, recovery, and post-session review rules.

  65. 65advanced · 28 min

    Final Audit + Maintenance Plan

    Re-audit patterns against an earlier baseline and schedule bounded maintenance, repair, and follow-up checks.

  66. 66advanced · 30 min

    Variant contracts: bounded budgets, interval cuts and subset routes

    Extend the restored problem-family series with bounded knapsack, two resource budgets, interval reconstruction and Held–Karp routes, using independent tiny oracles.