๐ฏ HOOK
The Magic Box
Imagine a magic box that gives you any item instantly if you know its number. Whether the box has 10 items or 10 million items, grabbing item #5 takes the exact same time.
๐งช EXPLORE
Instant Access Simulator
Try accessing an item from a small list vs a large list. Notice the speed!
Steps Taken
1
Time
Instant
๐ก EXPLAIN
What is O(1)?
Constant time means the operation takes the same amount of time regardless of the input size.
- โ Accessing an array index
- โ Hash map lookup (average)
- โ Basic math operations
- โ Pushing to a stack
# O(1) Python Example
def get_first_item(items):
return items[0]
โ๏ธ PRACTICE
Test Your Understanding
1. Which is O(1)?
๐