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[8] = {1, 3, -1, -3, 5, 3, 6, 7}, k = 3
Deque d = empty // holds indices
for (each i from 0 to 7)
if (d not empty AND front(d) <= i - k) popFront(d)
while (d not empty AND a[back(d)] <= a[i]) popBack(d)
pushBack(d, i)
if (i >= k - 1) Print a[front(d)]
end for- A. 3 3 5 5 6 6
- B. 3 -1 5 5 6 7
- C. 1 3 5 5 6 7
- D. 3 3 5 5 6 7
Correct answer: D. 3 3 5 5 6 7
The deque keeps indices of a decreasing run of values, so its front is the maximum of the current window of 3. Window maxima: [1 3 −1] 3, [3 −1 −3] 3, [−1 −3 5] 5, [−3 5 3] 5, [5 3 6] 6, [3 6 7] 7.
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? (& is AND, >> is right shift) Integer x = 23, t = 0 while ((x & 1) == 1) t = t + 1 x…
- What will be the output of the following pseudocode? (& is AND, | is OR, << and >> are shifts) Integer x = 43 Integer y = ((x & 170) >> 1)…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; & is AND, ^ is XOR, | is OR…
- What will be the output of the following pseudocode? Integer n = 3524, steps = 0 while (n != 6174) Integer big = digits of n sorted in…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer total = 0, n for (each…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; + joins strings) Function…