Question 71
HardWhat is the recursion depth of quick sort in the worst case and how can it be bounded to O(log n)?
- A.
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 recursion depth of quick sort in the worst case and how can it be bounded to O(log n)?
O(log n) always
O(n); recurse on the smaller part first and loop on the larger
O(n²)
O(1)
Sign up free to see the answer and a step-by-step explanation.
A recursive function calls itself twice on n/2 and does O(1) work. How many calls are made in total for n = 2^k?
2^k
k²
2^(k+1) − 1
k
Sign up free to see the answer and a step-by-step explanation.
What is the difference between backtracking and brute force?
They are the same
Brute force is always faster
Backtracking checks every complete candidate
Backtracking abandons a partial solution as soon as it can't lead to a valid one
Sign up free to see the answer and a step-by-step explanation.
How many subsets of {1, 2, …, 10} have a sum of exactly 10?
1
10
12
8
Sign up free to see the answer and a step-by-step explanation.
What does the 'rat in a maze' backtracking solution do when it reaches a dead end?
Starts again from the beginning
Breaks through the wall
Steps back to the previous cell and tries another direction
Stops completely
Sign up free to see the answer and a step-by-step explanation.
Which problem is NP-complete and commonly attacked with backtracking?
Finding the maximum of an array
Computing a prefix sum
Binary search
Graph colouring with k colours
Sign up free to see the answer and a step-by-step explanation.
Why does Python raise RecursionError for deep recursion while C may simply crash?
Python checks a recursion limit (default about 1,000); C relies on the OS stack, which overflows
They behave the same
C has no recursion
Python uses a heap for calls
Sign up free to see the answer and a step-by-step explanation.
How many distinct binary search trees can be made from keys 1 to 5?
32
120
42
14
Sign up free to see the answer and a step-by-step explanation.
Which recursion is tail-recursive?
def f(n): return f(n − 1) + f(n − 2)
def f(n): return 1 if n == 0 else n * f(n − 1)
def f(n, acc): return acc if n == 0 else f(n − 1, acc * n)
def f(n): return f(n // 2) + 1
Sign up free to see the answer and a step-by-step explanation.
What is the branching factor and depth of the recursion tree for generating all subsets of n elements (include/exclude)?
Branching n, depth 2
Branching n, depth n
Branching 1, depth n
Branching 2, depth n
Sign up free to see the answer and a step-by-step explanation.