ProgrammingHardAmazon OA · Capgemini
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Queue q = {1, 2, 3, 4, 5}
Integer k = 3
Stack st = empty
for (each i from 1 to k) push(st, dequeue(q))
while (st not empty) enqueue(q, pop(st))
for (each i from 1 to size(q) - k) enqueue(q, dequeue(q))
Print the queue from front to back- A. 4 5 3 2 1
- B. 1 2 3 5 4
- C. 5 4 3 2 1
- D. 3 2 1 4 5
Correct answer: D. 3 2 1 4 5
The first 3 go through a stack and come back reversed at the rear: 4 5 3 2 1. Then the 2 untouched items are rotated to the back: 3 2 1 4 5 — the first k elements reversed.
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? (All variables are integers, so / is integer division) // numbers stored as digit…
- What will be the output of the following pseudocode? // circular list of 6 people: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> back to 1 Node p = node 1…
- What is the time complexity of the following pseudocode, as a function of n? Function f(Integer n) if (n <= 1) return 1 end if return f(n…
- What is the time complexity of the following pseudocode, as a function of n? Procedure g(Integer n) if (n <= 1) return for (each i from 1…
- What will be the output of the following pseudocode? Integer m[3][3] = {{11, 2, 4}, {4, 5, 6}, {10, 8, -12}} Integer d1 = 0, d2 = 0 for…
- What will be the output of the following pseudocode? (/ truncates toward zero and mod keeps the sign of the dividend (as in C)) Integer x…