O(1) constant time
easy 13 minO(1) means an operation takes a fixed amount of work no matter how large the input is.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 12
What is the time complexity of accessing arr[5] in an array?
Which operation is NOT O(1)?
Why is hash map lookup O(1) on average?
What makes stack push and pop O(1)?
How does the cost of arr[999999] compare with arr[0]?
Which real-world action best represents O(1)?
What does the 1 in O(1) actually mean?
Why is the worst case of a hash table lookup O(n)?
Which operation is O(1) on a doubly linked list?
Why is inserting at index 0 of an array NOT O(1)?
Which Python operation is O(1)?
len(arr) on a Python list is O(1) because:
Pick an answer to see why.
Not quite. The address is calculated directly: base + 5 × element size, then one memory read. No iteration is needed.
Not quite. The maximum of unsorted data could be anywhere, so all n elements must be checked: O(n). The others are O(1) (the hash lookup on average).
Not quite. hash(key) mod table size gives the bucket index straight away. No searching through the other entries is needed.
Not quite. Push adds at the top and pop removes from the top. No other element is traversed or moved.
Not quite. Both are base + index × size followed by one read. The index value does not change the number of operations.
Not quite. A light switch is one action no matter how big the room is. Reading page by page and counting are O(n); the phone book is O(log n).
Not quite. O(1) means a fixed number of operations that does not grow with input size. It could be 1, 5 or 100.
Not quite. If every key collides into one bucket, the lookup must scan that bucket linearly. Good hash functions and resizing make this rare.
Not quite. With the pointer in hand you only update the prev and next links. The other three all require walking the list.
Not quite. Every one of the n elements moves one place to make room, so it is O(n). Only appending at the end is O(1) amortised.
Not quite. append adds at the end in O(1) amortised time. insert(0, x) is O(n), `in` on a list is O(n) and sort is O(n log n).
Not quite. A list stores its size as a field. len() returns that stored value instead of counting the elements.
Flashcards
Say your answer, then reveal it.
Card 1 of 11
What does O(1) mean in one sentence?
The work is bounded by a constant that does not grow with the input size n.
Say your answer, then reveal it.
Does O(1) mean exactly one operation?
No. It means a constant number of operations: 1, 5 or 100 steps are all O(1) as long as the count never depends on n.
Say your answer, then reveal it.
What is the formula behind O(1) array access?
address = base + index × element size. With base 1000 and 4-byte integers, arr[3] lives at 1012.
Say your answer, then reveal it.
Why is hash map lookup O(1) on average?
The hash function turns the key into a bucket index (hash mod table size), then one array access reaches the bucket, which holds only a few keys.
Say your answer, then reveal it.
When is a hash map lookup O(n)?
When many keys collide into one bucket, so a lookup scans them all. Good hash functions and resizing make this rare.
Say your answer, then reveal it.
What does amortised O(1) mean for list.append?
A single append can cost O(n) when the list resizes, but n appends cost O(n) in total, so each is O(1) on average over the sequence.
Say your answer, then reveal it.
Why are stack push and pop O(1)?
They only touch the top element and move the top pointer. Nothing below the top is moved or read.
Say your answer, then reveal it.
Why is len(arr) O(1) in Python?
The list stores its length as a field and updates it on every change. len() just reads that stored value instead of counting.
Say your answer, then reveal it.
Why is list.insert(0, x) not O(1)?
Every existing element must shift one place to the right, which is O(n). Only appending at the end is O(1) amortised.
Say your answer, then reveal it.
Which doubly linked list operation is O(1) given a pointer to the node?
Deleting that node: relink node.prev.next and node.next.prev. No traversal is needed.
Say your answer, then reveal it.
What is the golden rule of O(1)?
If you can calculate exactly where something is, you can reach it in O(1): arrays calculate an address, hash maps a bucket, stacks know the top.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.