Back to Blog

POV: The Elevator Stops at EVERY Floor πŸ›—πŸ˜€ (O(n) vs O(1) IRL)

Sandy LaneSandy Lane
β€’

Video: POV: The Elevator Stops at EVERY Floor πŸ›—πŸ˜€ (O(n) vs O(1) IRL) by Taught by Celeste AI - AI Coding Coach

Watch full page β†’

POV: The Elevator Stops at EVERY Floor πŸ›—πŸ˜€ (O(n) vs O(1) IRL)

Imagine you're running late and need to get to the 10th floor quickly. Taking the elevator that stops at every floor feels like an O(n) operationβ€”linear time where each stop adds delay. In contrast, taking the stairs is like O(1) constant time access: you go straight to your destination without interruptions.

Code

// O(n) - Elevator stops at every floor from 1 to 10
for (let floor = 1; floor <= 10; floor++) {
  elevator.stopAt(floor);  // Elevator must stop at each floor, causing delay
}

// O(1) - Stairs provide direct access to floor 10 without intermediate stops
stairs.goTo(10);  // Direct, constant time access with no stops

Key Points

  • O(n) complexity means the time grows linearly with the number of steps or stops.
  • The elevator example illustrates how stopping at every floor adds incremental delay.
  • O(1) complexity means constant time regardless of input size, like taking the stairs directly.
  • Understanding algorithm complexity helps explain real-world delays and efficiencies.