ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Stack s = empty, mins = empty
Procedure push2(Integer x)
push(s, x)
if (mins is empty OR x <= top(mins)) push(mins, x)
End Procedure
Procedure pop2()
if (top(s) == top(mins)) pop(mins)
pop(s)
End Procedure
push2(5) push2(3) push2(7) push2(3) pop2() Print top(mins)
pop2() pop2() Print top(mins)
push2(2) Print top(mins)- A. 3 5 2
- B. 3 3 2
- C. 5 5 2
- D. 3 7 2
Correct answer: A. 3 5 2
The second 3 was also pushed on mins (because of <=), so after the first pop the minimum is still 3. Popping 7 and then 3 leaves only 5 in both stacks: min 5. Pushing 2 makes it 2. Output 3 5 2.
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 a[8] = {1, 3, -1, -3, 5, 3, 6, 7}, k = 3 Deque d…
- 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…