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 a[6] = {4, 5, 2, 25, 7, 8}, res[6]
Stack st = empty // holds indices
for (each i from 0 to 5)
while (st not empty AND a[top(st)] < a[i])
res[pop(st)] = a[i]
end while
push(st, i)
end for
while (st not empty)
res[pop(st)] = -1
end while
Print res[0], res[2], res[4]- A. 5 5 8
- B. 25 25 8
- C. 5 25 -1
- D. 5 25 8
Correct answer: D. 5 25 8
The stack finds each element's next greater element to the right: 4 → 5, 2 → 25 (5 is to its left), 7 → 8. Output 5 25 8.
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? (Arrays are indexed from 0) Integer h[12] = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}…
- 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…