DEV Community

Sreekar Reddy
Sreekar Reddy

Posted on • Originally published at sreekarreddy.com

πŸ“ Big O Explained Like You're 5

Worst-case traffic time estimate

Day 40 of 149

πŸ‘‰ Full deep-dive with code examples


The Traffic Estimate

Your friend asks: "How long does your commute take?"

You could say:

  • "A short commute" (light traffic)
  • "A long commute" (worst case: traffic)

Big O is like worst-case estimate for algorithms!


Common Big O's

Big O Meaning Example
O(1) Constant Get item by index
O(log n) Logarithmic Binary search
O(n) Linear Loop through all
O(nΒ²) Quadratic Nested loops

What They Mean

O(1): Same time no matter the size

  • Get arr[5] with 10 items = Get arr[5] with 1 million items

O(n): Time grows with size

  • 10 items β†’ check 10
  • 1000 items β†’ check 1000

O(nΒ²): Time grows FAST

  • 10 items β†’ 100 operations
  • 1000 items β†’ 1,000,000 operations 😱

Visual

     |     O(nΒ²)
Time |   /
| O(n)     |
| ___ O(1) |
     +---------β†’
        Size
Enter fullscreen mode Exit fullscreen mode

In One Sentence

Big O describes how an algorithm's time grows as input size grows, focusing on worst-case scenarios.


πŸ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

Top comments (0)