Would this algorithm run in On?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
When someone asks whether an algorithm runs in O(n), the real task is to count how the work grows as input size grows. Linear time means the amount of work increases proportionally with the number of input elements, up to constant factors. To answer correctly, you need to identify the dominant operations, not just count the number of loops.
What O(n) actually means
An algorithm is O(n) if its running time grows at most linearly with input size n. That does not mean every line runs exactly once. It means the total amount of work is bounded by some constant multiple of n for large enough inputs.
For example, this is linear:
In the worst case, the loop inspects every element once, so the runtime is O(n).
Even this is still O(n):
There are two loops, but each loop is linear, so the total is O(n + n), which simplifies to O(n).
When an algorithm is not O(n)
The most common reason an algorithm is not linear is nested iteration over the same input.
This does n * n pairings, so the runtime is O(n^2), not O(n).
Another common trap is hidden work inside operations that look cheap. For example, membership checks on a Python list are linear:
The outer loop is linear, but value in seen is also linear in the size of seen, so the full algorithm becomes O(n^2) in the worst case.
If you switch to a set:
Now the membership check is typically constant time on average, and the algorithm becomes O(n).
How to analyze an algorithm properly
A reliable workflow is:
- Define what
nrepresents. - Count how many times the dominant operation runs.
- Replace low-level implementation details with their complexity.
- Drop constants and lower-order terms.
For example, suppose n is the number of elements in a list. If each element is touched once and each touch does constant-time work, the algorithm is linear.
But if each element triggers a full scan of the same list, the algorithm is quadratic. That is why "there is only one loop" is not enough to prove O(n).
Best case versus worst case
When people ask whether an algorithm "runs in O(n)," they usually mean worst-case time complexity unless stated otherwise.
Consider:
Best case: the first value is zero, so the runtime is constant.
Worst case: no value is zero, so every element is checked. That makes the worst-case time O(n).
Common Pitfalls
The biggest mistake is counting loops instead of operations. Two back-to-back loops can still be O(n), while one loop containing a linear-time operation can become O(n^2).
Another issue is ignoring the cost of library calls. Searching a list, concatenating strings repeatedly, or copying slices can change the true complexity.
Developers also forget to define n. In some problems, n might be the number of nodes, edges, characters, or matrix cells, and the complexity statement depends on that definition.
Finally, do not confuse upper bounds with exact runtime. Saying an algorithm is O(n) does not mean it is always proportional to n; it means it does not grow faster than linear asymptotically.
Summary
- '
O(n)means the runtime grows at most linearly with input size.' - Multiple linear passes are still
O(n). - Nested linear work often turns an algorithm into
O(n^2). - Hidden costs inside loop bodies matter as much as the loops themselves.
- Analyze dominant operations, not just the visual structure of the code.
Related reading
- Write a function that returns the longest palindrome in a given string
- Write a function to divide a number by 3 without using /, and operators. itoa available?
- Write a program to find 100 largest numbers out of an array of 1 billion numbers
- Writing a 36 bit random number generator
- Wrapping StopWatch timing with a delegate or lambda?
- writing tfrecord with multithreading is not fast as expected
- Writing an algorithm to decide whether a target number can be reached with a set of other numbers and specific operators?
- Writing your own square root function

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.