Last active
July 25, 2017 01:52
-
-
Save leovolving/4ddf1961c7f82e494133aa2adc41d7d7 to your computer and use it in GitHub Desktop.
Big O Drills
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Even or odd | |
| function isEven(value){ | |
| if (value % 2 == 0){ | |
| return true; | |
| } | |
| else | |
| return false; | |
| } | |
| //My answer: this function only takes a single input | |
| //The runtime is O(1) (constant time) | |
| //Are you here? | |
| function areYouHere(arr1, arr2) { | |
| for (let i=0; i<arr1.length; i++) { | |
| const el1 = arr1[i]; | |
| for (let j=0; j<arr2.length; j++) { | |
| const el2 = arr2[j]; | |
| if (el1 === el2) return true; | |
| } | |
| } | |
| return false; | |
| } | |
| //My answer: this function's runtime is dependent on the length of the arrays | |
| //The runtime is O(n) (linear time) | |
| //THINKFUL ANSWER: O^n (*nested loops*) | |
| //Doubler | |
| function doubleArrayValues(array) { | |
| for (let i=0; i<array.length; i++) { | |
| array[i] *= 2; | |
| } | |
| return array; | |
| } | |
| //My answer: this function's runtime is dependent on the length of the array | |
| //The runtime is O(n) (linear time) | |
| //Naive Search | |
| function naiveSearch(array, item) { | |
| for (let i=0; i<array.length; i++) { | |
| if (array[i] === item) { | |
| return i; | |
| } | |
| } | |
| } | |
| //My answer: this function's runtime is dependent on the length of the array | |
| //The runtime is O(n) (linear time) | |
| //Creating pairs: | |
| function createPairs(arr) { | |
| for (let i = 0; i < arr.length; i++) { | |
| for(let j = i+1; j < arr.length; j++) { | |
| console.log(arr[i] + ", " + arr[j] ); | |
| } | |
| } | |
| } | |
| //My answer: This function loops through the array twice | |
| //The runtime is O(n^2) (Polynomial time) | |
| //Computing fibonaccis | |
| //A fibonacci sequence is one where every number is the sum of the previous | |
| //two numbers in the sequence. For example the following is a fibonacci | |
| //sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34. The first number always starts at 1 (technically it is 0). | |
| //Then the second number is 0+1 = 1, the third number is the sum of the first and the second numbers (1 + 2 = 3) | |
| //and the sequence continues in a similar manner. | |
| //Here, we have a function generateFib that uses iteration to generate a fibonacci sequence. | |
| //Determine its run time complexity in big O. | |
| function generateFib(num) { | |
| let result = []; | |
| for (let i = 1; i <= num; i++) { | |
| // we're adding the first item | |
| // to the result list, append the | |
| // number 0 to results | |
| if (i === 1) { | |
| result.push(0); | |
| } | |
| // ...and if it's the second item | |
| // append 1 | |
| else if (i == 2) { | |
| result.push(1); | |
| } | |
| // otherwise, sum the two previous result items, and append that value to results. | |
| else { | |
| result.push(result[i - 2] + result[i - 3]); | |
| } | |
| } | |
| // once the for loop finishes | |
| // we return `result`. | |
| return result; | |
| } | |
| //My answer: This one is more complex but most of the items are constant, | |
| //so the runtime is mostly dependent on how high a number is given. | |
| //The runtime is O(n) (linear time) | |
| //An Efficient Search | |
| //In this example, we return to the problem of searching using a more | |
| //sophisticated approach than in naive search, above. | |
| //Assume that the input array is always sorted. | |
| function efficientSearch(array, item) { | |
| let minIndex = 0; | |
| let maxIndex = array.length - 1; | |
| let currentIndex; | |
| let currentElement; | |
| while (minIndex <= maxIndex) { | |
| currentIndex = Math.floor((minIndex + maxIndex) / 2); | |
| currentElement = array[currentIndex]; | |
| if (currentElement < item) { | |
| minIndex = currentIndex + 1; | |
| } | |
| else if (currentElement > item) { | |
| maxIndex = currentIndex - 1; | |
| } | |
| else { | |
| return currentIndex; | |
| } | |
| } | |
| return -1; | |
| } | |
| //My answer: This one is able to cut itself in half with each iteration, if needed | |
| //The runtime is O(log(n)) (logarithmic time) | |
| //Random element | |
| function findRandomElement(arr) { | |
| return arr[Math.floor(Math.random() * arr.length)]; | |
| } | |
| //My answer: The runtime is O(1) (constant time) | |
| //Is it prime? | |
| function isPrime(n) { | |
| // if n is less than 2 or a decimal, it's not prime | |
| if (n < 2 || n % 1 != 0) { | |
| return false; | |
| } | |
| // otherwise, check if `n` is divisible by any integer | |
| // between 2 and n. | |
| for (let i = 2; i < n; ++i) { | |
| if (n % i == 0) return false; | |
| } | |
| return true; | |
| } | |
| //My answer: This one is O(n) (linear time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment