Which of these list, set and deque operations are O(1)?
Question
A support bot keeps waiting tickets in a Python list called tickets. At peak it holds 100,000 tickets. For each line below, say whether it is O(1) or O(n), and why.
tickets.append(t) # 1 a new ticket arrives tickets.pop() # 2 tickets.pop(0) # 3 serve the oldest ticket tickets.insert(0, t) # 4 a VIP ticket jumps the queue tickets[50_000] # 5 t in tickets # 6 t in closed # 7 closed is a set queue.popleft() # 8 queue is a collections.deque queue[50_000] # 9 len(tickets) # 10
Which two lines would you change first, and to what?
Show hints (3)
- "PICTURE THE MEMORY"
A list keeps its items in one row of memory, in order. Which lines touch one slot, and which make the other items move?
- "ENDS AND MIDDLE"
Removing the last item moves nothing. Removing the first item shifts every other item left by one. A deque is built for both ends, but not for the middle.
- "JUMP OR SEARCH"
tickets[50_000]jumps straight to one slot.t in ticketscompares items one by one until it finds a match. A set finds an item by its hash instead.
Show checked explanation
O(1): append (amortized), pop(), tickets[50_000], t in closed (on average), popleft() and len. O(n): pop(0) and insert(0, t) shift every other item, t in tickets scans the list, and queue[50_000] walks into the deque. Fix line 3 first: keep the queue in a deque and use popleft(). Then replace line 6 with a set lookup.
from collections import deque closed = {"T2"} # a set: membership is O(1) on average queue = deque() for ticket in ["T1", "T2", "T3"]: if ticket not in closed: queue.append(ticket) # O(1) queue.appendleft("VIP") # O(1), unlike list.insert(0, x) while queue: print(queue.popleft()) # O(1), unlike list.pop(0)
VIP T1 T3
Code run on CPython 3.12 against the examples above. Checked by an editor other than the author.
Walkthrough
A list stores references to its items in one block of memory, in order. That one fact explains most of the answers.
| line | cost | why |
|---|---|---|
1 append(t) | O(1) amortized | a free slot usually waits at the end |
2 pop() | O(1) | nothing else moves |
3, 4 pop(0), insert(0, t) | O(n) | every other item shifts by one |
5 tickets[50_000] | O(1) | the slot's address is computed directly |
6 t in tickets | O(n) | compares items one by one |
7 t in closed | O(1) on average | the hash points to the right place |
8 popleft() | O(1) | a deque is built for both ends |
9 queue[50_000] | O(n) | the deque walks toward the middle |
10 len(tickets) | O(1) | the size is stored |
The front of a list is the trap. Serving 100,000 tickets with pop(0) shifts 99,999 items, then 99,998, and so on: 4,999,950,000 moves in all. The same work with popleft() takes 100,000 steps.
"Amortized" means averaged over many calls. A list keeps spare room at the end. When the room runs out, CPython asks for a block about one eighth bigger, and it may have to copy every item there. That one call can be O(n). But the spare room grows with the list, so 1,000,000 appends resize it fewer than 100 times. Spread over all the calls, each append costs O(1).
The set's O(1) is an average too. It hashes the item and looks in one place. If many items share a hash slot, a lookup slows toward O(n).
A deque is the opposite of a list. Both ends are O(1), but indexing into the middle is O(n). Keep lists where you need tickets[i], and use a deque where you add and remove at the front.
Common mistakes
- Using a list as a queue with
pop(0). It reads likepopleft(), but draining 100,000 tickets this way is about 50,000 times more work. - Thinking every
appendis fast. Now and then one call may copy the whole list to a bigger block. Total time stays linear, but a single call can be slow. - Replacing every list with a deque.
queue[i]in the middle of a deque is O(n), so binary search or random access on a deque gets slow. - Building a set for one lookup.
set(tickets)is itself O(n). A set pays off when you check membership many times.
Follow-up questions
- "What does
del tickets[50_000]cost on the 100,000-ticket list?" O(n). The 49,999 items after index 50,000 each shift left by one. Deleting near the end is cheap, and deleting near the front is expensive.tickets.remove(t)is O(n) too: it first searches fort, then shifts the rest. - "Is
key in some_dictO(1) as well?" Yes, on average. A dict finds keys by hash, like a set. Butvalue in some_dict.values()checks every value one by one, so it is O(n). If you search by value often, keep a second dict that maps values to keys.
What a strong answer covers
Explains each cost from how the structure is stored. Separates amortized from worst case for append, and average from worst case for sets. Picks a deque for the queue and a set for membership, and knows a deque is slow in the middle.
Open in interactive practice deck →