awesome-everything RU
↑ Back to the climb

Algorithms from zero

Thinking & complexity: predict then measure

Crux Hands-on project — implement algorithms of different complexity classes, benchmark them across growing input sizes, and confirm the measured growth matches the predicted Big-O.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at middle altitude — in the sky
◷ 200 min

Knowing that O(n²) “grows faster” than O(n) is not the same as watching it happen. Build a small benchmark harness, run several algorithms across growing input sizes, and see the curves bend exactly where Big-O said they would — analysis confirmed by measurement.

Goal

Turn the unit’s theory into an empirical loop: predict an algorithm’s Big-O by reading its code, measure how its real cost scales as n doubles, and reconcile the two — including the cases where constants and the time-space tradeoff make the practical winner differ from the asymptotic one.

Project
0 of 7
Objective

Build a 'predict-then-measure' benchmark that runs at least four algorithms of different complexity classes over a range of input sizes, then show that the measured growth (operation counts and timings) matches the Big-O you predicted from the code — and explain every place where it does not.

Requirements
Acceptance criteria
  • A predicted-vs-measured table: for each algorithm, the Big-O you predicted next to the doubling ratio you measured, with the two agreeing (O(n)≈2×, O(n²)≈4×, O(log n) a small additive step, O(1) flat).
  • The O(n²) algorithm's cost is shown overtaking the O(n) algorithm as n grows, with the crossover (and any small-n region where the 'slower' class wins due to constants) explicitly noted.
  • A short write-up reconciling every mismatch — e.g. noise at tiny n, constant factors, or a doubling ratio that only converges to the theoretical value as n grows large.
  • The duplicate-check comparison reports both time and extra memory for each approach and states which constraint (latency vs memory) would make you pick each one.
  • Your ~1-second prediction from the 10⁸ ops/sec rule lands within an order of magnitude of the measured result, with the gap explained (constant factors, language overhead).
Senior stretch
  • Plot the measured curves on the same axes and again on a log-log axis, where each Big-O class becomes a straight line whose slope is its exponent — read the slopes off and match them to the predictions.
  • Add the O(2ⁿ) naive recursive Fibonacci and a memoised O(n) version; show the exponential one becoming unrunnable past n ≈ 40 while the linear one stays instant.
  • Add a correctness harness: prove each algorithm with a stated specification and a battery of edge cases (empty input, single element, all-equal, all-negative) so you confirm speed without sacrificing correctness.
  • Repeat one benchmark in a second language (e.g. Python vs JS) and show that the doubling ratio — the Big-O fingerprint — is identical even though the absolute times differ, reinforcing that Big-O is machine- and language-independent.
Recap

This is the loop you will run whenever performance is in question: predict the Big-O by reading the code, then measure the real growth as n doubles and check the doubling ratio against the class. The asymptotic prediction almost always wins at scale, but constants and the time-space tradeoff decide the practical winner at the sizes you actually run — and a fast algorithm still has to be correct. Doing this once on a benchmark harness makes ‘estimate before you run’ a reflex.

Continue the climb ↑The array
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.