ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (number of ways to climb n stairs, taking 1 or 2 steps at a time)?
What will be the output of the following pseudocode (number of ways to climb n stairs, taking 1 or 2 steps at a time)?
Function climb(Integer n)
if (n <= 2)
return n
end if
Integer a = 1, b = 2
for (each i from 3 to n)
Integer temp = a + b
a = b
b = temp
end for
return b
End Function
Print climb(5), climb(8)- A. 8 34
- B. 5 13
- C. 8 21
- D. 13 34
Correct answer: A. 8 34
This is the Fibonacci-like staircase recurrence: climb(5)=8 and climb(8)=34, matching the Fibonacci sequence shifted by one position.
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 (product of array except self, without using division)? (Arrays are indexed from 0.)…
- What will be the output of the following pseudocode (how many output rows a zigzag string conversion uses)? Function numRowsUsed(String s…
- What will be the output of the following pseudocode (counting islands in a grid using flood-fill DFS)? (Grids are indexed from 0.) Integer…
- What will be the output of the following pseudocode (summing positive and negative matrix entries separately)? Integer mat[3][3] = {{1…
- What will be the output of the following pseudocode (counting distinct pairs whose difference is k)? (Arrays are indexed from 0.) Integer…
- What will be the output of the following pseudocode (2x2 matrix multiplication)? Integer A[2][2] = {{1, 2}, {3, 4}} Integer B[2][2] = {{5…