CMD Guide
HomeDSAFoundations

Linear Time On

Linear Time: O(n)

Imagine you are handed a stack of n exam papers and asked to find the highest score. There is no shortcut: you must look at every paper at least once, because any paper you skip could be the winner. If the stack doubles in size, your work roughly doubles too. That direct proportionality between the amount of input and the amount of work is exactly what we call linear time, written O(n).

The intuition is: touch each item a constant number of times. Whether you touch each item once, or twice, or five times, the total work still grows in a straight line as the input grows. That is why we drop constant factors and simply say O(n).

Precise definition

An algorithm runs in O(n) time if there exist constants c > 0 and n0 such that for all input sizes n ≥ n0, the number of basic operations T(n) satisfies T(n) ≤ c · n. In words: beyond some starting size, the runtime never exceeds a fixed constant times n.

Big-O is an upper bound. Constant factors and lower-order terms disappear: 3n + 7, n/2, and 100n + 5000 are all O(n), because for large n the leading n term dominates and the constants fold into c. The defining trait of linear time is that work scales in lock-step with input size — no exponents, no logarithms multiplying the n.

A worked example — counting the operations

Let's find the maximum of an array of n = 5 numbers: [4, 9, 2, 9, 6]. The algorithm keeps a running best and compares every element to it.

For n elements we do exactly n − 1 comparisons. For n = 5 that is 4; for n = 1000 it is 999. The count T(n) = n − 1 is bounded by c · n with c = 1, so this is O(n). Notice the answer is identical whether the max sits first or last — we still scan all n. That is why best, worst, and average cases here are all Θ(n) for n ≥ 1: the loop cannot terminate early without risking a missed maximum. Edge cases: n = 0 (empty) — no max; algorithm must define an error/optional return (0 comparisons). n = 1 — initialise best to the only element, 0 comparisons, still O(1) work and consistent with T(n)=n−1.

Common pitfalls & what an interviewer probes

When it matters + trade-offs vs neighbours

O(n) is the sweet spot for problems where you genuinely must inspect all the data: summing, filtering, a single pass to build a frequency map, or two-pointer / sliding-window techniques. It is usually the best achievable bound whenever the answer depends on every input element.

Rule of thumb at scale: for n = 107, an O(n) pass finishes in milliseconds, while O(n²) would need ~1014 operations — hours. That gap is why spotting a linear solution is so valuable.

Key takeaways

🤖 Don't fully get this? Learn it with Claude

Stuck on Linear Time On? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Linear Time On** (DSA) and want to truly understand it. Explain Linear Time On from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Linear Time On** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Linear Time On** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Linear Time On** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes