Fireship · @fireship

Big-O Notation in 100 Seconds

Uploaded December 18, 2019 · Length 1:40

Big-O Notation in 100 Seconds

Summary

big o notation explained in 100 seconds, including time and space complexity examples

  • big o notation
  • time complexity
  • space complexity
  • algorithm performance
  • data structures

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    Big O notation, a way to describe how well an algorithm performs as its input size grows.

  2. 0:05

    As we add more inputs or data points to an algorithm, there are two things that might

  3. 0:09

    grow in complexity.

  4. 0:10

    It may take longer to run the algorithm, which we call time complexity.

  5. 0:13

    Or it may require more memory, which we call space complexity.

  6. 0:16

    The variable n represents the number of inputs.

  7. 0:19

    Now imagine n being scaled up to infinity.

  8. 0:22

    The faster the result grows after each step, the worse your algorithm performs.

  9. 0:26

    In technical interviews, you'll often be asked to estimate the time or space complexity

  10. 0:29

    of the code you've implemented.

  11. 0:31

    Take for example a loop over an array of elements.

  12. 0:34

    It takes exactly one unit of time to loop over each element.

  13. 0:38

    This is known as linear complexity, because we have a perfect 1 to 1 relationship between

  14. 0:42

    the number of inputs and the amount of time it takes to execute the algorithm.

  15. 0:45

    But we can often do better than linear time.

  16. 0:48

    If we have a sorted array, we can implement a binary search, where we start in the middle,

  17. 0:52

    and then jump back and forth until we find the actual value we're looking for.

  18. 0:55

    This is known as logarithmic complexity, and it scales much better to a large number of

  19. 0:59

    inputs.

  20. 1:00

    At the most efficient end of the spectrum, we have constant time.

  21. 1:03

    An example is looking up an element in an array by its index.

  22. 1:06

    No matter how big that array is, it should always take the same amount of time.

  23. 1:10

    But things can also get less efficient.

  24. 1:12

    Imagine a loop within a loop.

  25. 1:13

    This is quadratic complexity, where the number of inputs is raised to the power of 2.

  26. 1:17

    And things can still get worse with exponential complexity.

  27. 1:20

    Imagine looping over every possible combination in an array.

  28. 1:23

    Some algorithms may have multiple different growth rates of complexity.

  29. 1:27

    Always simplify your notation down to the fastest growing, worst case scenario.

  30. 1:31

    This has been Big O Notation in 100 seconds.

  31. 1:34

    Thanks for watching, and I will see you in the next one.