ProgrammingHardAmazon OA · Capgemini
What will be the output of the following pseudocode? (operands are evaluated left to right; arrays are indexed from 0)
What will be the output of the following pseudocode? (operands are evaluated left to right; arrays are indexed from 0)
Integer q[5], front = 0, rear = 0, count = 0
Procedure enq(Integer x)
q[rear] = x
rear = (rear + 1) mod 5
count = count + 1
End Procedure
Function deq()
Integer v = q[front]
front = (front + 1) mod 5
count = count - 1
return v
End Function
enq(1) enq(2) enq(3) enq(4)
deq() deq() deq()
enq(5) enq(6) enq(7)
Print front, rear, deq()- A. 3 7 4
- B. 0 2 1
- C. 3 2 5
- D. 3 2 4
Correct answer: D. 3 2 4
After 4 enqueues rear = 4; three dequeues move front to 3. Enqueuing 5, 6, 7 writes q[4], q[0], q[1] and rear wraps to 2. The next dequeue returns q[3] = 4: 3 2 4.
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? String in = "A+BC-D/E", out = "" Stack st = empty // prec: and / are 2, + and - are 1…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; heap[k / 2] is the parent of…
- What will be the output of the following pseudocode? Integer table[7] = all -1, probes = 0, h for each key in {50, 700, 76, 85, 92, 73…
- What is the time complexity of the following pseudocode, as a function of n? for (i = 1; i < n; i = i 2) for (j = 0; j < i; j = j + 1)…
- What is the time complexity of the following pseudocode, as a function of n (n > 2)? while (n > 2) n = squareRoot(n) // integer square…
- What will be the output of the following pseudocode? Integer coins[3] = {1, 2, 5} Integer ways[0..6] = all 0 ways[0] = 1 for (each a from…