First step before you optimize code, is determining how fast your code is with
help of big O notation (make a judgment call to our code). If your code is
slow, you need to dig deeper and research for faster solutions.
Mean average of even numbers
Efficiency of this code?
How many steps this algorithm take for N data elements in the worst case?
Please take into account all operations such as assignment, arithmetic, etc.
For N data elements this code will take 3N steps.
World builder
What is the time complexity of this algorithm?
Time complexity of this algorithm is O(N2).
What is the time complexity of this algorithm?
Time complexity of this algorithm is O(N3).
Array sample
Can you identify the time complexity of this algorithm?
Function ends up taking the same number of steps (~3) no matter what N is
(number of elements in the array).
So complexity is O(1).
Average Celsius reading
What is its Big O?
O(N), we have 2 loops, first loop generate about N steps (if we
ignore insertion), second N steps (and few constant steps), and by using Big O
notation this will be reduced from 2N into O(N).
Clothing labels
We need to generate 5 size variations of each item in the input array.
What is the time complexity of this algorithm?
Results:
This code runs around 5N times, the inner loop will always run five times no
matter what N is. This is reduced to O(N), since Big O notation ignores
constants.
Count the ones
What’s Big O of this algorithm (input - N, is matter)?
The outer loop is iterating over the inner arrays, and the inner loop is
iterating over the actual numbers. At the end of the day, our inner loop only
runs for as many numbers as there are in total. Because of this, we can say
that N represents how many numbers there are. And since our algorithm simply
processes each number, the function’s time complexity is O(N).
Palindrome checker
A palindrome is a word or phrase that reads the same both forward and backward.
Some examples include “racecar,” “kayak,” and “deified.”
Determine the Big O of this algorithm, how many steps does it take?
The guts of the algorithm takes place within the while loop. Now, this loop is
somewhat interesting because it only runs until it reaches the midpoint of the
string. That would mean that the loop runs N/2 steps (if we ignore some
steps). However, Big O ignores constants. Because of this, we drop the division
by 2, and our algorithm is O(N).
Get all the products
How many steps does this algorithm take, and what is its Big O?
About (2N2).
Because Big O ignores constants, we express this as O(N2).
Dealing with multiple datasets
How to express this in Big O?
O(N∗M), can be construed as a range between O(N) and O(N2).
Here’s a tale of two scenarios:
In Scenario 1, there are two arrays of size 5, total steps is 25.
In Scenario 2, there’s one array of size 9, and another of size 1, total steps is 9.
Higher we see order of magnitude.
Whenever we have two distinct datasets that have to interact with each other
through multiplication, we have to identify both sources separately (N and
M) when we describe the efficiency in terms of Big O.
Password cracker
Here we use brute force to find the password.
How many steps does this algorithm take?
a-z ~ 26 steps, number of character in alphabet.
We can express this algorithm steps as 26n.
if n=2, 26∗26=676 steps,
if n=3, 26∗26∗26=17576 steps.
TODO: write own solution in Python.
In a certain sense, O(2N) is the opposite of O(logN). With an algorithm of
O(logN) (like binary search), each time the data is doubled, the algorithm
takes one additional step. With an algorithm of O(2N), each time we add one
element of data, the algorithm doubles in steps!
Use Big O Notation to describe the time complexity of the following function.
The function returns true if the array is a “100-Sum Array,” and false if it is
not.
A “100-Sum Array” meets the following criteria:
Its first and last numbers add up to 100.
Its second and second-to-last numbers add up to 100.
Its third and third-to-last numbers add up to 100, and so on.
Here, N is the size of the array.
O(N), here we do about 4N/2 steps. In worst keys all array numbers
are 50.
Use Big O Notation to describe the time complexity of the following function. It
merges two sorted arrays together to create a new sorted array containing all
the values from both arrays:
First you need to determine the size of the array, which is N, suppress the
desire to guess the complexity, you need to analyze.
It’s slightly tricky to define N in this case, since we are dealing with two
distinct arrays. However, the algorithm only processes each value once, so we
could decide to call N the total number of values between both arrays, and the
time complexity would be O(N).
If we want to be more literal, and call one array N and the other M, we
could alternatively express the efficiency as O(N+M). However, because we’re
simply adding N and M together, it’s simpler to just use N to refer to the
total number of data elements across both arrays and call it O(N).
Use Big O Notation to describe the time complexity of the following function.
This function solves a famous problem known as “finding a needle in the
haystack.” (иголка в стоге сена).
Both the needle and haystack are strings. For example, if the needle is “def”
and the haystack is "abcdefghi", the needle is contained somewhere in the
haystack, as "def" is a substring of "abcdefghi". However, if the needle is
“dd”, it cannot be found in the haystack of "abcdefghi".
This function returns true or false, depending on whether the needle can be
found in the haystack:
In a worst-case scenario, this algorithm runs for the number of characters in
the “needle” multiplied by the number of characters in the “haystack.” Since the
needle and haystack may have different numbers of characters, this is
O(n∗m)
Use Big O Notation to describe the time complexity of the following function.
How many times the innermost loop run?
This function finds the greatest product of three numbers from a given array:
N is the size of the array, and the time complexity is O(N3), as it’s processed
through triply-nested loops. Really, the middle loop runs N/2 times, and the
innermost loop runs N/4 times, so this is N∗(N/2)∗(N/4), which is N3/8
steps. But we drop the constant, leaving us with O(n3)
Describe the efficiency of this function in terms of Big O:
N is the size of the resumes array. Since in each round of the loop we
eliminate half of the resumes, we have an algorithm of O(logn).