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()
  1. A. 3 7 4
  2. B. 0 2 1
  3. C. 3 2 5
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic