Question 21
EasyWhat is the maximum recursion depth of recursive binary search on an array of n elements?
- A.
O(n²)
- B.
O(1)
- C.
O(log n)
- D.
O(n)
Sign up free to see the answer and a step-by-step explanation.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the maximum recursion depth of recursive binary search on an array of n elements?
O(n²)
O(1)
O(log n)
O(n)
Sign up free to see the answer and a step-by-step explanation.
How many subsets (including the empty set) does a set of 3 elements have?
6
9
8
3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def r(s):
return s if len(s) <= 1 else r(s[1:]) + s[0]
print(r('abc'))
cba
abc
bca
cab
Sign up free to see the answer and a step-by-step explanation.
In backtracking, what is pruning?
Abandoning a partial solution as soon as it can't lead to a valid answer
Sorting the choices
Deleting the input
Repeating the same choice
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def f(n):
return n if n < 10 else f(n // 10)
print(f(4721))
4721
1
472
4
Sign up free to see the answer and a step-by-step explanation.
What is the main drawback of recursion compared with a loop?
Recursion can't use parameters
Each call uses stack memory, and very deep recursion can overflow the stack
Recursion is never correct
Recursion can't return values
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def g(a, b):
return a if b == 0 else g(b, a % b)
print(g(36, 60))
12
180
36
6
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def perms(a, k=0):
if k == len(a):
return 1
c = 0
for i in range(k, len(a)):
a[k], a[i] = a[i], a[k]
c += perms(a, k + 1)
a[k], a[i] = a[i], a[k]
return c
print(perms([1, 2, 3]))
6
4
3
8
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def subsets(a):
if not a:
return [[]]
rest = subsets(a[1:])
return rest + [[a[0]] + s for s in rest]
print(len(subsets([1, 2, 3])))
3
9
8
6
Sign up free to see the answer and a step-by-step explanation.
How many moves does the Tower of Hanoi need for n disks?
n²
2ⁿ − 1
n!
2n
Sign up free to see the answer and a step-by-step explanation.