Fireship · @fireship
Big-O Notation in 100 Seconds
Uploaded December 18, 2019 · Length 1:40
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.
- 0:00
Big O notation, a way to describe how well an algorithm performs as its input size grows.
- 0:05
As we add more inputs or data points to an algorithm, there are two things that might
- 0:09
grow in complexity.
- 0:10
It may take longer to run the algorithm, which we call time complexity.
- 0:13
Or it may require more memory, which we call space complexity.
- 0:16
The variable n represents the number of inputs.
- 0:19
Now imagine n being scaled up to infinity.
- 0:22
The faster the result grows after each step, the worse your algorithm performs.
- 0:26
In technical interviews, you'll often be asked to estimate the time or space complexity
- 0:29
of the code you've implemented.
- 0:31
Take for example a loop over an array of elements.
- 0:34
It takes exactly one unit of time to loop over each element.
- 0:38
This is known as linear complexity, because we have a perfect 1 to 1 relationship between
- 0:42
the number of inputs and the amount of time it takes to execute the algorithm.
- 0:45
But we can often do better than linear time.
- 0:48
If we have a sorted array, we can implement a binary search, where we start in the middle,
- 0:52
and then jump back and forth until we find the actual value we're looking for.
- 0:55
This is known as logarithmic complexity, and it scales much better to a large number of
- 0:59
inputs.
- 1:00
At the most efficient end of the spectrum, we have constant time.
- 1:03
An example is looking up an element in an array by its index.
- 1:06
No matter how big that array is, it should always take the same amount of time.
- 1:10
But things can also get less efficient.
- 1:12
Imagine a loop within a loop.
- 1:13
This is quadratic complexity, where the number of inputs is raised to the power of 2.
- 1:17
And things can still get worse with exponential complexity.
- 1:20
Imagine looping over every possible combination in an array.
- 1:23
Some algorithms may have multiple different growth rates of complexity.
- 1:27
Always simplify your notation down to the fastest growing, worst case scenario.
- 1:31
This has been Big O Notation in 100 seconds.
- 1:34
Thanks for watching, and I will see you in the next one.


