Multiple Choice Questions Related To Testing Knowledge About Time and Space Complexity of A Program
Multiple Choice Questions Related To Testing Knowledge About Time and Space Complexity of A Program
If you
are still doing those then message us ASAP.
Notes about Week 1: If you are still doing pre x sum problems, that is completely ne and you
can also refer to this weeks material in parallel. If you found those problems too easy or too
di cult or just want more practice with pre x sums, refer to the optional ones.
(do problems 1,4 if you want a challenge; do problems 2,3,INOI(1) if you want easier ones/more
practice).
Everyone here is on di erent levels, so let us know if the problems we’re asking you to do are
too easy or too di cult and we will change them for you accordingly.
Hi fellow programmers,
We are trying to create a multiple choice quiz for space and time complexity of the programs
related questions. Here are a set of 20 questions we collected. Please feel free to give your
answers to these questions. Any feedback about the set of questions. Please also feel
propose to any more set of MCQs that you would like to add here, there might be some
interesting questions that you might have encountered during the programming and would
like to add here
Q1.
Average case time complexity of quicksort?
A. O(n)
B. O(n log n)
C. O(n2 )
D. O(n3 )
Q2.
Worst case time complexity of quicksort?
A. O(n)
B. O(n log n)
C. O(n2 )
D. O(n3 )
Q3.
Time complexity of binary search?
A. O(1)
B. O(log n)
2
C. O((log n) )
D. O(n)
Q4.
def f()
ans = 0
for i = 1 to n:
for j = 1 to log(i):
ans += 1
print(ans)
A. O(n)
B. O(n log n)
C. O(n2 )
D. O(n3 )
Q5.
def f():
a = 0
for i = 1 to n:
a += i;
b = 0
for i = 1 to m:
b += i;
A. O(n)
B. O(m)
C. O(n + m)
D. O(n ∗ m)
Q6.
def f():
a = 0
for i = 1 to n:
a += random.randint();
b = 0
for j = 1 to m:
b += random.randint();
A. O(n)
B. O(m)
C. O(n + m)
D. O(n ∗ m)
Q7.
def f():
int a[n][n]
// Finding sum of elements of a matrix that are above or on the dia
sum = 0
for i = 1 to n:
for j = i to n:
sum += a[i][j]
print(sum)
A. O(n)
B. O(n log n)
C. O(n2 )
D. O(n3 )
Q8.
def f():
int a[n][n]
sum = 0
// Finding sum of elements of a matrix that are strictly above the
for i = 1 to n:
for j = i to n:
sum += a[i][j]
print(sum)
for i = 1 to n:
sum -= a[i][i]
A. O(n)
B. O(n log n)
C. O(n2 )
D. O(n3 )
Q9.
def f():
ans = 0
for i = 1 to n:
for j = n to i:
ans += (i * j)
print(ans)
A. O(n)
B. O(n log n)
C. O(n2 )
D. O(n3 )
Q10.
def f():
int a[N + 1][M + 1][K + 1]
sum = 0
for i = 1 to N:
for j = i to M:
for k = j to K:
sum += a[i][j]
print(sum)
Q11.
def f(n):
ans = 0
while (n > 0):
ans += n
n /= 2;
print(ans)
A. O(log n)
B. O(n)
B. O(n log n)
C. O(n2 )
Q12.
A. O(log2 n)
B. O(log3 n)
C. O(log10 n)
D. O(n)
Q13.
def f():
ans = 0
for (i = n; i >= 1; i /= 2):
for j = m to i:
ans += (i * j)
print(ans)
A. O(n + m)
B. O(n ∗ m)
C. O(m log n)
D. O(n log m)
Q14.
def f():
ans = 0
for (i = n; i >= 1; i /= 2):
for (j = 1; j <= m; j *= 2)
ans += (i * j)
print(ans)
A. O(n ∗ m)
B. O(log m log n)
C. O(m log n)
D. O(n log m)
Q15.
Let n = max{a, b}
A. O(1)
B. O(log n)
C. O(n)
D. O(n2
Q16.
// Binary searching in sorted array for finding whether an element exis
def exists(a, x):
// Check whether the number x exists in the array a.
lo = 0, hi = len(a) - 1
while (lo <= hi):
mid = (lo + hi) / 2
if (a[mid] == x): return x;
else if (a[mid] > x): hi = mid - 1;
else lo = mid + 1;
return -1; // Not found.
Let n = len(a)
A. O(1)
B. O(log n)
C. O(n)
D. O(n2
Q17.
// Given a sorted array a, nd the number of occurrence of number x in the entire array.
Let n = len(a)
A. O(1)
B. O(log n)
C. O(n)
D. O(n2
Q18.
// Finding fibonacci numbers.
def f(n):
if n == 0 or n == 1: return 1
return f(n - 1) + f(n - 2)
A. O(log n)
B. O(n)
C. O(n2 )
D. O(2n )
Q19.
A. O(log n)
B. O(n)
C. O(n2 )
D. O(2n )
Q20.
def f(a):
n = len(a)
j = 0
for i = 0 to n - 1:
while (j < n and a[i] < a[j]):
j += 1
Q21.
def f():
ans = 0
for i = 1 to n:
for j = i; j <= n; j += i:
ans += 1
print(ans)
A. O(log n)
B. O(n)
C. O(n log n)
D. O(n2 )
Wouldn’t question 5 depend on the bigger number(n or m)? In that case it could be either a
or b, right?
1B
2C
3B
4B
5C
6D
7C
8C
9C
10 B
11 A
12 C
13 C
14 B
15 B (No idea, though i guess this)
16 B
17 B
18 D
19 B
20 B
21 C
1b
2c
3b
4b
5c
6d
7c
8c
9c
10 b
11 a
12 c
13 c
14 b
15 b
16 b
17 b
18 d
19 b
20 b
21 a
Question 13:
If n is large, and m is small, then the innermost statement is executed n + n/2 + n/4 +
… times.
So the problem is at least .
If n and m are large, then the innermost statement is executed roughly m \log n times.
The only choice available that is big enough is B: {\scr O}(n \times m).
Question 21:
Has no options C, and two options D. The correct choice is the rst option D!
admin Jan '18
As both n and m are the input parameters of the function. So, we should give O(n + m) as
time complexity.
Really?
I guessed 2nd and 15th.