Complexity & Linear Structures
The first algorithmic skill is translating constraints into a cost budget and preserving a simple invariant. Arrays, hashing, windows and queues expose these habits with minimal abstraction.
Section 1 of 5 · 8 units · about 35 hours
By the end you can Solve two tag-hidden linear-structure problems, state each invariant before coding, and use randomized tests to expose and minimize one plausible wrong solution.
Units
- Cost models, asymptotic notation and cases4h
- Aggregate, accounting and potential amortized analysis4h
- Loop invariants, brute-force oracles and differential tests4h
- Arrays, strings, locality and linked structures5h
- Hash tables: collisions, load factor, resizing and tombstones4h
- Stacks, queues, deques and monotone structures5h
- Two pointers and sliding windows5h
- Prefix sums, difference arrays and prefix plus hash4h
Build it
Mini project · about 8 to 12 hours
Hash map, two tag-hidden linear problems and a shrunk wrong solution
Build an open-addressing hash map checked against dict with randomized operation histories. Solve two tag-hidden linear-structure problems from invariants stated before code. Shrinking exposes one plausible wrong solution, and a probe-cost prediction tested by measurement closes the loop on the cost model.
- 1Implement a linear-probing hash map with
get,put, tombstonedeleteand resizing at a stated load factor. Write its probe invariant and the amortized cost ofputfrom the doubling argument. - 2Test it with a Hypothesis rule-based state machine against a
dictoracle, checking the probe invariant and the load factor after every step. Draw colliding keys so probe chains and tombstones are exercised. - 3Seed one defect of each kind, a
deletethat empties the slot and aresizethat copies slots by index. Show the state machine fails on each, and keep the shrunk histories as regression tests. - 4Solve LC 1248 Count Number of Nice Subarrays with tags hidden. Write the counting invariant and the time and space bounds first, then check the result against an brute force.
- 5Solve LC 862 Shortest Subarray with Sum at Least K with tags hidden. First write the shrinking-window attempt and let Hypothesis shrink a failing input against brute force, then solve with prefix sums and a monotone deque and state the broken assumption.
- 6Predict probe counts across load factors and key distributions, then measure and compare the random-key rows against the linear-probing estimate. Record predictions beside measurements and explain the gaps.
What you hand in
- The hash map module with its invariant, cost notes, state machine and seeded-defect histories.
- Both LC solutions with invariants and bounds stated first, differential tests and the shrunk counterexample.
- A probe-count table with predictions beside measurements and the comparison against the estimate.
How to check it
- Self-grade with the rubric below
- LeetCode 1248: Count Number of Nice Subarrays
Submit after your own differential test passes.
- LeetCode 862: Shortest Subarray with Sum at Least K
Submit the deque solution, not the window.
How it's graded
Hash map correctness
Pass: The state machine agrees with the dict oracle on randomized histories with colliding keys, and both seeded defects fail with shrunk histories.
Great: Each shrunk history is short and readable, kept as a regression test and mapped to the invariant clause it broke.
Invariants before code
Pass: Both solutions state the invariant and the time and space bounds before the code, and differential tests against brute force pass.
Great: The invariants are also asserted in a debug mode that the randomized tests switch on.
Wrong solution exposed
Pass: Hypothesis shrinks a failing window input, and the write-up states the assumption the counterexample breaks.
Great: A property test on restricted inputs shows where the same window stays correct.
Probe measurement
Pass: The probe table covers several load factors and key distributions, with predictions recorded before measuring and random-key rows compared against the estimate.
Great: A timing comparison against dict names the setup and explains the gap from implementation costs.
Resources
- DSA Handbook - Foundations and linear patternsHandbook Academy
Study Part 0, Part 1, Part 3 and Part 4 in order, plus the Part 5 Linked-list fundamentals chapter. Use the routing-rule pages for arrays versus lists, map choice, stack versus queue, two pointers, sliding windows and prefix methods before seeing solved code.
- AlgoMaster DSA - linear structures sliceAlgoMaster
Attempt Design HashMap, Longest Substring Without Repeating Characters and Sliding Window Maximum cold. Hide tags, state the invariant first, and add randomized operation histories to Design HashMap.
- MIT 6.042J - asymptotics and invariantsMIT OpenCourseWare
Use the asymptotics, state-machine invariants and proof-method readings when a cost or correctness argument is weak. Solve only the matching problems.
- Algorithms lecture notes - amortized analysis and hashingJeff Erickson
Use note 9 Amortized Analysis for the summation, taxation, charging and potential-function proofs, and note 5 Hashing for chaining, universal hashing and open-addressing analysis, when the handbook treatment is too brief for a proof.
- Algorithmica - memory and data layoutSergey Slotin
Read the memory hierarchy, cache and complexity-model chapters, then measure one contiguous-versus-linked and one hash-versus-ordered workload crossover.
- Hypothesis documentation - Quickstart and stateful testingHypothesis
Follow Quickstart for
@givenstrategies and the pytest combination, Configuring test settings formax_examplesandderandomize, then Rule-based state machines (rule,precondition,invariant,run_state_machine_as_test) for randomized operation histories againstdictanddequeoracles. - NeetCode - unseen linear problemsNeetCode
Use only for two tag-hidden transfer problems after the AlgoMaster slice. Do not start another full roadmap or watch a solution before the attempt is complete.
- CPython source - list and dict objectsPython Software Foundation
Read
list_resizeinObjects/listobject.cfor the growth formula and pattern comment, and theUSABLE_FRACTIONdefinition, perturbation-probing comment and dummy-entry handling inObjects/dictobject.c. Read the cited functions and comments only.