Big-O and Complexity
Predict how fast code is before you run it — time and space complexity, common classes, and how to analyse any loop.
- Module 6 of 11
- Intermediate
- 11 chapters
- 55 min
Every coding interview asks for the complexity of your solution. Big-O lets you predict how code scales before you run it and pick an algorithm from the input limits.
What you will be able to do
- Work out the time and space complexity of any loop
- Analyse recursive code
- Know the cost of everyday Java operations
- Choose an approach from the problem's constraints
Chapters
1. Count Steps, Not SecondsThe input size, usually called n, is how big the input is: the number of items in an array, characters in a string, nodes in a graph.4 min2. Growth Rates and Big-OBig-O is about large n. For n — 10, an O(n²) algorithm with small constants can beat an O(n log n) one. Interview inputs are usually5 min3. Logarithms From Zerolog₂ n ("log base 2 of n") — the power you raise 2 to, to get n. Since 2¹⁰ = 1,024, log₂ 1,024 = 10.3 min4. Rules for Analysing CodeWorking out a travel time: steps done one after another add up ("30 min to the station + 2 hours on the train"); a step done for every6 min5. Best, Worst and Average Case; Ω and ΘWorst case — the maximum steps over all inputs of size n. This is what interviewers mean by default. It's a guarantee.3 min6. Space ComplexitySpace complexity — how much extra memory an algorithm uses as n grows, in Big-O.3 min7. Amortized AnalysisYour phone plan costs ₹0 for 29 days and ₹3,000 on the 30th. Is it an expensive phone plan? Per day, it's ₹100 — the occasional big payment,3 min8. Complexity of RecursionA recursive function calls itself on a smaller input (fully taught in 11recursion.md).6 min9. Costs of Everyday Java OperationsMemorise this table. Hidden costs here (Ch 4, Rule 7) are the most common source of wrong complexity answers. The internals behind every3 min10. From Constraints to AlgorithmBefore a trip, you check how far it is. 2 km — walk. 20 km — take a scooter. 2,000 km — you need a plane. The distance tells you which vehicle4 min11. Beyond Big-OBig-O is the most important tool, but not the only one:2 min
Extras
The Whole StoryThe whole module retold as one story — no new facts, a fast revision of Big-O and Complexity.RevisionInterview QuestionsReal interview questions on Big-O and Complexity, answered the way you would say them out loud.Interview prepQuick ReferenceGlossary and cheat sheet for Big-O and Complexity — the page to reread before an interview.Reference