Comparing Run Times

Practice Questions

Computer Science › Comparing Run Times

Page 1 of 2
10 of 11
1

Consider the following code:

O_n_

What is the expected runtime of the code (in Big-O notation?)

2

Consider the following code:

O_2n_

What is the run time of the code (in Big-O notation?)

3

Consider the following code:

O_n3_

What is the run time of the code (in Big-O notation)?

4

for( int i = 0; i < n; ++i){

for( int j = 1; j < n; j *= 2){

someFunction();

}

}

For the code above, what is the run time in Big O notation?

5

What is the BigO of the following function?

public void bigO(int[][] m)

{

if (m.length > 2)

{

for (int i = 0; i < m.length - 1; i++)

{

for (int j = 0; j < m[i].length; j++)

{

System.out.println(m[i][j]);

}

}

}

else

{

for (int j = 0; j < m[0].length; j++)

{

System.out.println(m[0][j]);

}

}

}

6

Consider the following code:

O_log_n__

What is the run-time of the code (in Big-O notation?)

7

Which is more efficient?

a)

arr = [0, 1, 2, 3]

for (int i = 0; i < arr.length; i++) {

int j = 0;

if (j == arr[i]) {

j++;

}

}

b)

ArrayList<Integer> arL = new ArrayList<Integer>();

arL.add(0);

arL.add(1);

arL.add(2);

arL.add(3);

for (int i = 0; i < arL.size(); i++) {

int k = 0;

if (k == arL.get(i)) {

k++;

}

}

8

True or False.

The code snippet A has a more efficient running time than code snippet B.

(A)

for (int i = 0; i < 25; i++) {

for (int j = i; j < 25; j++) {

}

}

(B)

for (int i = 0; i < 25; i++ {

for (int j = 0; j < 25; j++) {

}

}

9

Which is more efficient (i.e. Lower Big O)?

arr = \[1, 2, 3, 4, 5, 6, 7, 8\]

arr2 = \[\[1,2\],\[3,4\],\[5,6\], \[7,8\], \[9,10\], \[10, 11\]\]

for (int i = 0; i < arr.length; i++) {

for (int j = i; j < arr2.length; j++) {

arr\[i\]\[j\] = 0;

}

}

arr = \[1, 2, 3, 4, 5, 6, 7, 8\]

arr2 = \[\[1,2\],\[3,4\],\[5,6\], \[7,8\], \[9,10\], \[10, 11\]\]

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr2.length; j++) {

arr\[j\] = 0;

}

}

10

Which has faster compile time, O(N), O(N2), O(N3), or O(NlogN)?

Page 1 of 2
Return to subject