March 24
O(1)
What O(1) means in practice and why some operations take the same amount of time no matter how large the input gets.
What it means
O(1) means the operation takes constant time.
The input size does not meaningfully change the cost.
That is different from O(n), where the work grows with the size of the input.
Common example in JavaScript
Looking up a key in a Map or checking existence in a Set is usually O(1):
const seen = new Set<number>()
seen.add(7)
seen.has(7)
Scanning an array to find the same value would be O(n).
Why it matters
O(1) is often what lets you replace repeated scans with fast memory.
That tradeoff is one of the most common interview optimizations.
Better question
Instead of only asking “is this O(1)?”, ask:
- what information am I trying to retrieve quickly?
- can I store it while I iterate?
- is the extra memory worth the time saved?
Share this page
Copy the link manually from the field below.