ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer coins[3] = {1, 2, 5}
Integer ways[0..6] = all 0
ways[0] = 1
for (each a from 1 to 6)
for (each i from 0 to 2)
if (coins[i] <= a)
ways[a] = ways[a] + ways[a - coins[i]]
end if
end for
end for
Print ways[6]- A. 5
- B. 15
- C. 13
- D. 8
Correct answer: B. 15
With the amount in the OUTER loop, ways[a] counts ordered sequences of coins (1 + 2 and 2 + 1 differ): ways = 1, 1, 2, 3, 5, 9, 15 for 0 to 6. The usual coin-change count (5 unordered ways) needs the coin loop outside.
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? (code(c) gives a letter's character code; strings are indexed from 0) String s =…
- What will be the output of the following pseudocode? // list: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL Node slow = head, fast = head while…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Integer j for (each d from 0 to…
- What will be the output of the following pseudocode? (strings are indexed from 0) Procedure perm(String s, Integer l) // s is passed by…
- What will be the output of the following pseudocode? (>> is right shift and & is bitwise AND; (mask >> i) & 1 is evaluated first) Integer…
- What will be the output of the following pseudocode? Integer n = 6, count = 0 Integer col[n] Function ok(Integer r, Integer c) for (each k…