ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode (Pascal's identity for combinations)?
What will be the output of the following pseudocode (Pascal's identity for combinations)?
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
Print C(6, 2)- A. 12
- B. 15
- C. 20
- D. 6
Correct answer: B. 15
This is Pascal's triangle recurrence for nCr. C(6,2) = 6!/(2!*4!) = 15, which the recursive breakdown via C(n-1,r-1)+C(n-1,r) also correctly builds up to.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode (Armstrong number check)? Function isArmstrong(Integer n) String s = toString(n)…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 3) for (each j from 1 to 3) if (j == 2) break end…
- What will be the output of the following pseudocode (Kadane's maximum subarray sum)? (Arrays are indexed from 0.) Integer arr[9] = {-2, 1…
- What is the time complexity of binary search on a sorted array of n elements? Function binarySearch(Array arr, Integer target) Integer lo…
- What is the worst-case time complexity of the following pseudocode (bubble sort) on an array of n elements? for (each i from 0 to n - 2)…
- What is the time complexity of the following recursive pseudocode (computing n choose k via Pascal's identity), in the worst case, as a…