O(n) denotes linear time complexity, where the execution time increases directly with the input size, while O(log n) indicates logarithmic time complexity, which grows more slowly as the input size increases. O(n) is common in algorithms that require a complete traversal of data, like searching through an unsorted list, whereas O(log n) is typical in algorithms that divide the problem space, such as binary search on a sorted array.
In depth
O(n) and O(log n) represent fundamentally different approaches to algorithm efficiency. O(n) implies that for every additional element in your input, the time taken to process increases proportionately, often seen in operations like linear searches or iterating through arrays. In contrast, O(log n) describes algorithms that efficiently reduce the problem size, exemplified by binary search, where each step eliminates half of the remaining candidates. This makes logarithmic algorithms highly suitable for large datasets, as they scale much better than linear algorithms when the input grows significantly. Understanding these nuances can shape how one designs systems for performance, balancing complexity and runtime efficiency.
Example scenario
Consider a system that needs to look up user records in a database. If the records are unsorted, a linear search through the list of users would take O(n) time since every record must be checked. However, if the database uses indexing on a sorted list of users, a binary search approach can significantly speed up lookups to O(log n), allowing the system to quickly pinpoint a user record even with millions of entries, enhancing overall performance.
Common mistakes
A common mistake is confusing linear time complexity with logarithmic time complexity and underestimating the impact on performance. Many candidates will describe O(n) as more efficient than O(log n) without recognizing that O(log n) is rarely affected by input size increases beyond a certain point. Another mistake is failing to consider the underlying data structure; for example, assuming a linear search is always appropriate without acknowledging that sorted arrays offer more efficient searching with logarithmic time complexities.
Follow-up questions
- Can you explain how you would implement a binary search algorithm?
- What are the trade-offs of using O(log n) algorithms in terms of setup or preconditions?
- How does input data structure affect the choice between O(n) and O(log n)?
- Have you encountered edge cases that complicate these time complexities?

Preparing for a system design interview?
- A 7-step method for answering any system design question, plus estimation numbers
- 5 worked designs: URL shortener, rate limiter, chat, news feed and file storage
- Guides to caching, databases, queues, consistency and reliability, with a 4-week study plan
- 306 practice questions with answers on caching, databases, queues, microservices, APIs and more
5 worked designs, a 4-week plan and 306 practice questions with answers.
The material stays free to read online. This is the organised, offline, print-ready PDF edition. Secure payment by PayPal; your personal download link is emailed after payment is confirmed. Digital product, no shipping.