1.9k views

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);
f(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$
edited | 1.9k views
0
What's problem with c option ?
0
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
Q=1,2,3
Not empty so in
i=1
Q=2,3
Call it self Q=2,3
Check if empty
Not empty in
i=2
Q=3
Call it self Q=3
Check if empty
Not empty in
i=3
Q=empty
Call itself Q=empty
Check if Q empty
Is empty return
Enqueue(i)
Q=3
Return
enqueue(i)
Q=3,2
Return
enqueue(i)
Q=2,3,1
Return
Exit

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...
0
The question never said C language. We should follow the terminology in question and not think about any implementation.
0

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.

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'.

0
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."
0
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.
0
@Anand Mundhe

why are you behaving like a nerd ?
0
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 ....

Happy?

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

edited by
0
@ 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.
0
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?
0
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.
0
any reference ? I donot think call by reference or call by value will make any problem here.
0
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???

explanation...

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)) remains.it continues till 5 is deleted and again activation is executed by inserting q(5)...to q(1),,,thus reversing the queue
0
is ny other better approach to understand it ?
0
why it is saved... it is not declared as static and default everything is in activation record please explain this point .
–1
Call by value.....

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

0
Only happens if call by reference..... can u see & anywhere?
ans b)
–1
Wrong
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.

1
2