Code Monkey home page Code Monkey logo

big_o_notations-brief's Introduction

Big-O Notation Practice

Step One: Simplifying Expressions

Simplify the following big O expressions as much as possible:

  1. O(n + 10) O(n)

  2. O(100 * n) O(n)

  3. O(25) O(1)

  4. O(n^2 + n^3) O(n^3)

  5. O(n + n + n + n) O(n)

  6. O(1000 * log(n) + n) O(n)

  7. O(1000 * n * log(n) + n) O(n log(n))

  8. O(2^n + n^2) O(2^n)

  9. O(5 + 3 + 1) O(1)

  10. O(n + n^(1/2) + n^2 + n * log(n)^10) O(n^2)


Step Two: Calculating Time Complexity

Determine the time complexities for each of the following functions.

function logUpTo(n) {
  for (let i = 1; i <= n; i++) {
    console.log(i);
  }
}

Time Complexity: O(n)

function logAtLeast10(n) {
  for (let i = 1; i <= Math.max(n, 10); i++) {
    console.log(i);
  }
}

Time Complexity: O(n)

function logAtMost10(n) {
  for (let i = 1; i <= Math.min(n, 10); i++) {
    console.log(i);
  }
}

Time Complexity: O(1)

function onlyElementsAtEvenIndex(array) {
  let newArray = [];
  for (let i = 0; i < array.length; i++) {
    if (i % 2 === 0) {
      newArray.push(array[i]);
    }
  }
  return newArray;
}

Time Complexity: O(n)

function subtotals(array) {
  let subtotalArray = [];
  for (let i = 0; i < array.length; i++) {
    let subtotal = 0;
    for (let j = 0; j <= i; j++) {
      subtotal += array[j];
    }
    subtotalArray.push(subtotal);
  }
  return subtotalArray;
}

Time Complexity: O(n^2)

function vowelCount(str) {
  let vowelCount = {};
  const vowels = "aeiouAEIOU";

  for (let char of str) {
    if(vowels.includes(char)) {
      if(char in vowelCount) {
        vowelCount[char] += 1;
      } else {
        vowelCount[char] = 1;
      }
    }
  }

  return vowelCount;
}

Time Complexity: O(n)

Part 3

  1. True or false: n^2 + n is O(n^2). True - ( + n becomes insignificant)

  2. True or false: n^2 * n is O(n^3). True

  3. True or false: n^2 + n is O(n). False

  4. What’s the time complexity of the .indexOf array method? O(n)

  5. What’s the time complexity of the .includes array method? O(n)

  6. What’s the time complexity of the .forEach array method? O(n)

  7. What’s the time complexity of the .sort array method? O(n log(n)) to O(n^2) without optimization

  8. What’s the time complexity of the .unshift array method? O(n)

  9. What’s the time complexity of the .push array method? O(1)

  10. What’s the time complexity of the .splice array method? O(n)

  11. What’s the time complexity of the .pop array method? O(1)

  12. What’s the time complexity of the Object.keys() function? O(n)


Space Complexity

  1. What’s the space complexity of the Object.keys() function? O(n)

big_o_notations-brief's People

Contributors

blinter avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.