ProgrammingHardAmazon OA · TCS Digital

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer cost[3] = {10, 15, 20}
Integer a = 0, b = 0, c         // cheapest cost to stand on step i - 2 and i - 1
for (each i from 2 to 3)
    c = min(b + cost[i - 1], a + cost[i - 2])
    a = b
    b = c
end for
Print b
  1. A. 15
  2. B. 10
  3. C. 20
  4. D. 25

Correct answer: A. 15

You may start on step 0 or 1 and climb 1 or 2 steps, paying the cost of each step you leave. Reaching the top (past step 2) is cheapest by starting at step 1 and paying 15 to jump two steps: 15.

More Pseudocode and Code Tracing questions

Exams that ask this topic