ProgrammingHardTCS NQT · Accenture pattern
What is the time complexity of the following recursive pseudocode (computing n choose k via Pascal's identity), in the worst case, as a function of n?
What is the time complexity of the following recursive pseudocode (computing n choose k via Pascal's identity), in the worst case, as a function of n?
Function C(Integer n, Integer r)
if (r == 0 or r == n)
return 1
end if
return C(n - 1, r - 1) + C(n - 1, r)
End Function- A. O(n)
- B. O(n log n)
- C. O(2^n)
- D. O(n^2)
Correct answer: C. O(2^n)
Without memoization, this naive recursive Pascal's-triangle computation branches into 2 calls at almost every step, recomputing many overlapping subproblems; its worst-case call count grows exponentially, O(2^n).
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What is the space complexity (auxiliary stack space) of the following recursive factorial function for input n? Function fact(Integer n)…
- What will be the output of the following pseudocode (anagram check via sorting)? Function isAnagram(String a, String b) return…
- What will be the output of the following pseudocode (duplicate detection using a set)? Function hasDuplicate(Array arr) Set seen = empty…
- What will be the output of the following pseudocode (maximum sum of any window of size k)? (Arrays are indexed from 0.) Integer arr[6] =…
- What will be the output of the following pseudocode (counting inversions, brute force)? (Arrays are indexed from 0.) Integer arr[5] = {2…
- What will be the output of the following pseudocode (counting pairs summing to a target, brute force)? (Arrays are indexed from 0.)…