Coder Social home page Coder Social logo

david-kariuki / ace-the-java-coding-interview Goto Github PK

View Code? Open in Web Editor NEW
46.0 1.0 10.0 441 KB

Java project to learn advanced Data Structures and Algorithms

License: MIT License

Java 100.00%
dsa dsa-learning-series dsa-practice dsa-project dsalgo-questions

ace-the-java-coding-interview's Issues

Challenge 3: Big O of Nested Loop with Multiplication

Compute the Big O of an algorithm that involves nested loops, and increments the loop variables through multiplication.

public static void main(String[] args) {
		int n = 10; // O(time complexity of the called function)
		int sum = 0; //O(?)
		double pie = 3.14; //O(?)
		int var = 1; //O(?)

		while(var < n) {
			System.out.println("Pie: " + pie); //O(?)
			for (int j = 0; j < var; j++) {
				sum++; //O(?)
			}
			var *= 2;  //O(?)
		} //end of while loop
		System.out.println("Sum: " + sum); //O(?)
	}

Challenge 2: Big (O) of Nested Loop with Subtraction

Compute Big(O) of an algorithm that involves nested loops and the loop variables decrement.

public static void main(String[] args) {
    int n = 10; // O(time complexity of the called function)
    int sum = 0; //O(1)
    double pie = 3.14; //O(1)
    
    //O(?)
    for (int var = n; var >= 1; var = var - 3) {
      System.out.println("Pie: " + pie);
      //O(?)
      for (int j = n; j >= 0; j = j - 1) {
        sum++;
      }
    } //end of outer for loop
  System.out.println("Sum: " + sum);//O(1)
  }

Challenge 7: Nested Loop with Multiplication (Pro)

Compute the big O complexity of the code snippet given below.

public static void main(String[] args) {
    int n = 10;   
		int sum = 0;  
    int j = 1;   
		double pie = 3.14; 

		for (int var = 0; var < n; var++) {  
      
			System.out.println("Pie: " + pie); 
			while(j < var) { 
        sum += 1;  
        j *= 2;  
      }
    } 
    System.out.println("Sum: " + sum); 
  }

Challenge 6: Nested Loop with Multiplication (Advanced)

Compute the Big O complexity of the code snippet given below.

public static void main(String[] args) {
    int n = 10;   
		int sum = 0; 
		double pie = 3.14; 

		for (int var = 0; var < n; var++) {   
      int j = 1;  
			System.out.println("Pie: " + pie);
			while(j < var) { 
        sum += 1;  
        j *= 2;  
      }
    } //end of for loop
    System.out.println("Sum: " + sum); 
  }

Challenge 4: Nested Loop with Multiplication (Basic)

A basic exercise based on the Big(O) of an algorithm that involves nested loops and the loop variables increment with multiplication.

public static void main(String[] args) {
		int n = 10; // O(time complexity of the called function)
		int sum = 0; //O(1)
		double pie = 3.14; //O(1)
		int var = 1; //O(?)
		while(var < n) { 
			System.out.println("Pie: " + pie); 
				//O(?)
			for (int j = 1; j < n; j = j + 2) {   
				sum++;  
			}
			var *= 3;  
		} //end of while loop
		System.out.println("Sum: " + sum); //O(1)
	}

Challenge 5: Nested Loop with Multiplication (Intermediate)

Compute the Big O complexity of the code snippet given below. It is better to solve it on a piece of paper, and then see if your answer matches the correct option!

public static void main(String[] args) {
    int n = 10;    //O(1) 
    int sum = 0;  //O(1)
    int j = 1;   //O(1)
    double pie = 3.14;  //O(1) 
  
    //O(?)
    for (int var = 1; var < n; var += 3) {
      System.out.println("Pie: " + pie);
      j = 1;
      while (j < n) { //O(?)
        sum += 1;     
        j *= 3;       
      }
    }
    System.out.println("Sum: " + sum); //O(1)
	}

Challenge 1: Big (O) of Nested Loop with Addition

Compute the Big O complexity of an algorithm that involves nested loops where the loop variables increase with addition.

public static void main(String[] args) {
    int n = 10;
    int sum = 0;
    double pie = 3.14;
    
    for (int var = 0; var < n; var = var + 3) {
      System.out.println("Pie: " + pie);
      for (int j = 0; j < n; j = j + 2) {
        sum++;
        System.out.println("Sum = " + sum);
      }
    }
  }

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.