A light switch
O(1) — Constant
The analogy
Flipping a light switch takes the same moment whether the house has one bulb or a thousand. The size of the problem simply does not enter into it.
Visualizer
The light switch
step 1 / 9SWITCH0
1
2
3
4 bulbs, one switch. Flipping it is one operation.n = 4, operations = 1
def toggle(lights):
lights.on = not lights.on # one operation
# n = 4 bulbs -> 1 operation
# n = 1_000_000 -> 1 operation
d = {"k": 1}
d["k"] # O(1)
a[i] # O(1)
stack.append(x) # amortised O(1)Check yourself
Which is O(1)?