We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

O(n) - Order ā€œnā€

O(n) is very common - When the number of steps in an algorithm grows at the same rate as its input size, it's classified as O(n)

For example, our find min algorithm from earlier is O(n):

  1. Set min to positive infinity.
  2. For each number in the list, compare it to min. If it is smaller, set min to that number.
  3. min is now set to the smallest number in the list.

The input to the find min algorithm is a list of size n. Because we loop over each item in the input once, we add one step to our algorithm for each item in our list.

As we use find min with larger and larger inputs, the length of time it takes to execute the function grows at a steady linear pace. We can reasonably estimate the time it will take to run, based on a previous measurement. If we find that:

Input size Time to run
find_min(10 items) 2 ms

Then we can estimate the following:

Input size Time to run
find_min(100 items) 20 ms
find_min(1000 items) 200 ms
find_min(10000 items) 2000 ms

Assignment

LockedIn users want to know which of their followers has the highest engagement score.

Complete the find_max function. It should take a list of integers and return the largest value in the list.

The "runtime complexity" (aka Big O) of this function should be O(n).

Don't exclude negative values from the result.