ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (Arrays are indexed from 0)
What will be the output of the following pseudocode? (Arrays are indexed from 0)
Integer h[12] = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}
Integer lmax[12], rmax[12], w = 0
lmax[0] = h[0]
for (each i from 1 to 11)
lmax[i] = max(lmax[i - 1], h[i])
end for
rmax[11] = h[11]
for (each i from 10 down to 0)
rmax[i] = max(rmax[i + 1], h[i])
end for
for (each i from 0 to 11)
w = w + min(lmax[i], rmax[i]) - h[i]
end for
Print w- A. 5
- B. 8
- C. 6
- D. 10
Correct answer: C. 6
Water above bar i is min(tallest bar on its left, tallest on its right) − h[i]. Adding up: 1 (index 2) + 1 + 2 + 1 (indices 4–6) + 1 (index 9) = 6.
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) String s = "PROGRAMMING", r = "" Boolean…
- What will be the output of the following pseudocode? String s = " the quick brown fox " Integer words = 0 Boolean inWord = false for (each…
- What will be the output of the following pseudocode? Integer val[7] = {1000, 500, 100, 50, 10, 5, 1} String sym[7] = {"M", "D", "C", "L"…
- What will be the output of the following pseudocode? (code(c) gives a character's code; 'A' to 'Z' have consecutive codes) String a =…
- What will be the output of the following pseudocode? (Strings are indexed from 0) String s = "BANANAS" Integer best = 1, l, r, n = 7 for…
- What will be the output of the following pseudocode? (& is AND, | is OR, << and >> are shifts; 15 = 0000 1111 and 240 = 1111 0000) Integer…