Problem Set 2: Practice Problems Relating To Analysis Notations
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) {
count = j = 0;
do {
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
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
Question 8
(/learn)
void someMethod(int n) {
Question 9
← 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)