Sunday, July 10, 2011

Character Queue - Quiz

The problem

A few days ago, I saw the following question in the programming forums.


Consider the queue (q) of characters where it is a straight queue which is allocated 8 characters:

Front=2 rear=5 q= -,-,A,B,C,-,-,-

(for notational convenience “-” is used to denote an empty cell)

The following operations have to be performed.

(i) D is added to the queue
(ii) Two characters are deleted from the queue
(iii) E and F are added to the queue
(iv) One character is deleted from the queue
(v) G and H are added to the queue

What are the intermediate correct front and rear values when the above operations are performed?

Choose at least one answer.

A. front=5, rear=8
B. front=3, rear=6
C. front=2, rear=6
D. front=4, rear=7
E. front=4, rear=6


Step by Step trace

The problem can be solved by a step by step trace. The first two steps is done below.
It is not difficult to derive the solution after a full trace is done.
index all the cells :
queue   : -,-,A,B,C,-,-,-
index   : 0,1,2,3,4,5,6,7
pointer :     F     R

Step (i) D is added to the queue
queue   : -,-,A,B,C,D,-,-
index   : 0,1,2,3,4,5,6,7
pointer :     F       R

Step (ii) Two characters are deleted from the queue
queue   : -,-,-,-,C,D,-,-
index   : 0,1,2,3,4,5,6,7
pointer :         F   R


Verify the answer


After deriving the solution, you may want to verify the answer. For such a simple problem, it is very easy to make a quick prototype. Compile and Run the program to verify your answer.

No comments:

Post a Comment