0% found this document useful (0 votes)
92 views

Problem Set 2: Practice Problems Relating To Analysis Notations

This document contains 9 questions related to analysis notations and time complexity. Question 1 asks about explaining why a statement about an algorithm's lower bound makes no sense. Questions 2 through 9 ask about determining the time complexity of various code snippets.

Uploaded by

RonaldMartinez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Problem Set 2: Practice Problems Relating To Analysis Notations

This document contains 9 questions related to analysis notations and time complexity. Question 1 asks about explaining why a statement about an algorithm's lower bound makes no sense. Questions 2 through 9 ask about determining the time complexity of various code snippets.

Uploaded by

RonaldMartinez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Problem Set 2

Practice problems relating to analysis notations.

Question 1

Suppose your friend discovers a new algorithm and in his excitement tells
you that his algorithm has a lower bound of O(n2). Can you explain why
your friend's statement makes no sense?

Question 2
Does O(22n) equal O(2n) ?

Question 3

Give an example of an algorithm whose best case is equal to its worst case?

Question 4

Work out the time complexity for the algorithm given below:

(/learn)
void averager(int[] A) {

float avg = 0.0f;


int j, count;

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


avg += A[j];
}

avg = avg / A.length;

count = j = 0;

do {

while (j < A.length && A[j] != avg) {


j++;
}

if (j < A.length) {
A[j++] = 0;
count++;
}
} while (j < A.length);
}

Question 5

Q
What is the complexity of the below snippet

(/learn)
for( int i=0; i<array.length; i++){
for(int j=0; j<10000; j++)
{
// some useful work done here.
}
}

Your Answer

A) O(n)

Explanation

Even though the loops are nested but the inner loop isn’t
dependent on the size of the input array. It’ll always run for
ten thousand iterations whether the input size is ten, ten
thousand or a million. Therefore, if it seems counterintutive
but the complexity of this snippet is O(n).

B) O(n2)

Explanation

Even though the loops are nested but the inner loop isn’t
dependent on the size of the input array. It’ll always run for
ten thousand iterations whether the input size is ten, ten
thousand or a million. Therefore, if it seems counterintutive
but the complexity of this snippet is O(n).

(/learn)
C) O(1000*n)
Explanation

Though this answer is technically correct but when talking


about asymptotic behavior we tend to drop the constants.
Therefore the complexity is O(n).

Great, you got it right!

Retake Quiz

Question 6

Consider the following snippet of code and determine its running time
complexity?

(/learn)
void complexMethod(int[] array) {
int n = array.length;
int runFor = Math.pow(-1, n) * Math.pow(n, 2);
for (int i = 0; i < runFor; i++) {
System.out.println("Find how complex I am ?")
}
}

Question 7

Determine the time complexity for the following snippet of code

void complexMethod(int n, int m) {

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


for (int i = 0; i < m % n; i++) {
System.out.println("")
}
}
}

For non-java folks, m % n notation means m modulus n.

Question 8

Determine the time complexity for the following snippet of code

(/learn)
void someMethod(int n) {

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


for (int i = 0; i < 3; i++) {
for (int i = 0; i < n; i++) {
System.out.println("I have 3 loops");
}
}
}
}

Question 9

Determine the time complexity for the following snippet of code

void someMethod(int n, int m) {

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


for (int i = 0; i < m; i++) {
System.out.println("I have 2 loops");
}
}
}

←    Back Next    →
(/courses/big- (/courses/big-
MARK AS COMPLETED
o- o-
notation- notation-
for- for-
interviews- interviews-
Small omega and Small o Notations Solution Set 2
and- and-
beyond/gxMKl3W1lM9) beyond/m2MJD192GN9)

Ask a Question
Report
(https://discuss.educative.io/c/big-o-for-coding-interviews-and-beyond-c-h-
an Issue
afzal/formal-analysis-tools-problem-set-2)
(/learn)

You might also like