The Gateway to Computer Science Excellence
+26 votes

Suppose you are given an implementation of a queue of integers. The operations that can be performed on the queue are:

  1. $isEmpty (Q)$ — returns true if the queue is empty, false otherwise.
  2. $delete (Q)$ — deletes the element at the front of the queue and returns its value.
  3. $insert (Q, i)$ — inserts the integer i at the rear of the queue.

Consider the following function:

void f (queue Q) {
int i ;
if (!isEmpty(Q)) {
   i = delete(Q);
   insert(Q, i);

What operation is performed by the above function $f$ ?

  1. Leaves the queue $Q$ unchanged
  2. Reverses the order of the elements in the queue $Q$
  3. Deletes the element at the front of the queue $Q$ and inserts it at the rear keeping the other elements in the same order
  4. Empties the queue $Q$
in DS by Boss (16.3k points)
edited by | 2.8k views
What's problem with c option ?
Recursive. Hmm good... so you think c do call by reference? I'm surprised ....
Its call by value... so fuction will not reverse.....
First call will take element at last and put to first and sends deleted copy to next recursion

Suppose queue contain. 1,2,3
execution start
Now first check if empty
Not empty so in
Call it self Q=2,3
Check if empty
Not empty in
Call it self Q=3
Check if empty
Not empty in
Call itself Q=empty
Check if Q empty
Is empty return

So now you see only first element go to last .... so please fix it... if someone write wrong answer in gate.. they will be mad at you guys..... so ... let me know if something wrong here

So correct answer should be(must be) c..
C. Delets element at front of queue and insert it to tail keeping other elements sequence as it is

And sorry for bad language... I'm not good in english but I can code...
The question never said C language. We should follow the terminology in question and not think about any implementation.

Well then tell me, dear if this is not a program,

Quote/Moto from my Idle:

So when a question of programming is given, one should compile it in the brain and execute it, line by line to get an accurate answer.

If you do not think of implementation, then you are doing it wrong.

Most of the times your answer will be wrong in exams.

so about the language, it is not a c language then c++, but c++ require & to show references...

if java(Same goes for C#) then according to recommendation class name should start with a capital, So we cannot say 'queue' is a user defined class. if it is a class then - bro how can it be in gate exam... are the gate examiners don't have programming knowledge..

This one is a tricky question, so a perfect programmer will put 'B', and marks get reduced. But the programmers who still work after perfection will see the answer as 'C'...

I'm not an expert in it or something... it just I confirmed it from many programmers. And all of them say 'C'.

Forget to mention

question is: "Suppose you are given an [<implementation>] of a queue of integers"

how can you say: "and not think about any implementation."
Good to know that you're a perfect programmer :)

And no where in the question it asks to tie the implementation to any particular language. Be confident; but not be overconfident.
@Anand Mundhe

why are you behaving like a nerd ?
Ohh, sorry sorry... It was just . We had this question in exam, n someone reffered all of us students to this page. And ya, I lost my mind for the answer... it is true that we shouldn't link this program to any language. Insted we can call is as pseudo code and move on...
Sorry for being nerd/rude(or being myself).....

After 2-3 days of reverse engineering on this question, we all concluded that.
1. This is not a complete program(not a program will better), because if we try to thik it as a language of 'c' then this code is simply of infinity loop..
2. considering this code as pseudo code, the answer is b, it reverses the queue ....


6 Answers

+30 votes
Best answer

$insert()$ will inserts the value in just reverse order.

Correct Answer: $B$

by Veteran (119k points)
edited by
@ shreshta I have a doubt i.e as Q is not a global structure or static so how various invocation can work on the same queue q. As every invocation will work on their local copies so how an inner invocation can change the value of the queue of the calling function(or outer function).So I think the answer must be c because only the outermost function will have an impact on the original queue.
Can we not do push, push , pop in one stack? then in case of poping only one element is popped. Then why same will not happen in queue?
Because every function invocation will be doing changes to their own local copies which are provided by their parent function and not on the same queue because it is not called by reference but call by value. That's why only the changes made by the outermost invocation i.e the parent or root will be done on the original queue.
any reference ? I donot think call by reference or call by value will make any problem here.
The above answer will be alright if we consider it to be global so that all invocations will be able to make changes to the same Queue. Have you considered it to be global???
Arrays  in C are passes as reference so it shouldn't be a problem. Ofcourse I'm making an assumption that Queue is implemented using array.
+8 votes
answer will be b.


assume a queue of element 1 2 3 4 5...

now as Q is not empty it will delete 1 and 1 will be sored in i and den again f(Q) will be called which contains element 23456...but the trace (activation of inset (Q,1)) continues till 5 is deleted and again activation is executed by inserting q(5) q(1),,,thus reversing the queue
by Boss (16.2k points)
is ny other better approach to understand it ?
why it is saved... it is not declared as static and default everything is in activation record please explain this point .
Call by value.....
+5 votes

Answer should be B because Deletion operation is performed until all elements are deleted and any insert function not invoked until all deletion operation completed when it is completed function returns and insert operation is called then element pushed into the queue in this way we get reverse of the elements of the queue Q 

option B is correct

by Boss (11.6k points)
Only happens if call by reference..... can u see & anywhere?
+3 votes
ans b)
by Loyal (5.2k points)
0 votes
In this recursion, queue is being deleted by one element every time and getting saved in i. And again the function calls itself. When queue becomes empty, the last element will be inserted first in the queue. This will be for all the elements present. Thus, it reverses the order of elements in the queue.
by Active (4.7k points)
–2 votes
Answer: B
by Boss (33.9k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,301 answers
104,999 users