DSA · Patterns, not puzzles
What is Big O notation and why does it matter?
Short answer
Big O describes how an algorithm's time or space grows as the input size grows, ignoring constants and lower-order terms. It compares scalability rather than exact runtime on one machine.
Big O answers one question: when the input gets larger, how does the work grow? A function that doubles its work when the input doubles is linear. A function with nested iterations over the same input is quadratic. Constants and small terms are dropped because they stop mattering as the input becomes large.
The growth rates you should recognise
- O(1): constant time, such as reading an array index
- O(log n): halving the search space, such as binary search
- O(n): one pass over the input
- O(n log n): comparison sorting
- O(n^2): nested loops over the same input
- O(2^n): exploring every subset
function containsDuplicate(values) {
for (let i = 0; i < values.length; i++) {
for (let j = i + 1; j < values.length; j++) {
if (values[i] === values[j]) return true;
}
}
return false;
}You can often improve the complexity by changing the data structure. A hash set turns the duplicate check into one pass, giving O(n) time at the cost of O(n) space. That trade-off is usually the real interview discussion.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between Big O, Big Omega and Big Theta?
- 2Why do we drop constants and lower-order terms?
- 3What is the complexity of the hash set version of this function?
- 4When does Big O fail to predict real performance?
Say it in the interview
Big O describes how the work grows as the input grows, not the exact number of milliseconds. I drop constants and lower-order terms because the dominant term decides the trend. For example, nested loops over the same array are quadratic, while a single pass with a hash set is linear. The notation helps me reject approaches that will not scale, but I still measure when constant factors matter.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live DSA mock interview and get scored feedback on depth, clarity and confidence.