Skip to content

Instantly share code, notes, and snippets.

@tparveen
Last active July 10, 2021 00:03
Show Gist options
  • Save tparveen/226ba96056a62e815f964b9f6cce3b12 to your computer and use it in GitHub Desktop.
Save tparveen/226ba96056a62e815f964b9f6cce3b12 to your computer and use it in GitHub Desktop.
Big O exercises

Exercise: What's the complexity?

Implement the following algorithms and analyze its expected (average) and worst run time complexities. You should work through the functions one by one, making comments and identifying the complexity of each part of the algorithm. If you are unsure of how the algorithms scale, then try adding counter variables and incrementing them each time an operation takes place. By logging out the counters for different sizes of input you should be able to see a trend developing.

Exercise 1:

Write a program that determines if an input is even or odd. Explain its average and worst run time

Exercise 2:

Write a program that doubles every value in an array. Explain its average and worst run time

Exercise 3:

Write an algorithm to find an element randomly in the given array. Explain its average and worst run time

Exercise 4:

Write an algorithm that creates pairs that you can work with every day. Each pair be only printed once and you can't pair with yourself. Explain your algorithm's average and worst run time

var people = ["JP", "Dennell", "Benjamin", "Paul", "Donna", "Emily"];

function createPairs (arr){

}

createPairs(people);

Exercise 5:

Write an algorithm to determine if a given number is prime or not. Explain its average and worst run time

Exercise 6:

Given two arrays, write an algorithm to determine whether values from the first array is present in the second array. Explain its average and worst run time

Exercise 7:

Write an algorithm that finds the element with the minimum value in an array. Explain its average and worst run time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment