52 52 votes Suppose you are given an implementation of a queue of integers. The operations that can be performed on the queue are:$\text{isEmpty (Q)}$ — returns true if the queue is empty, false otherwise.$\text{delete (Q)}$ — deletes the element at the front of the queue and returns its value.$\text{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$ ?Leaves the queue $Q$ unchangedReverses the order of the elements in the queue $Q$Deletes the element at the front of the queue $Q$ and inserts it at the rear keeping the other elements in the same orderEmpties the queue $Q$ Data Structures gateit-2007 data-structures queue normal + – Ishrat Jahan 24.9k views answer comment Share Follow Print See all 13 Comments 13 13 Comments reply Show 10 previous comments Leeaa commented Dec 30, 2020 reply Follow flag @Anand Mundhe According to me, implementing using arrays : call by reference is default. implementing using linked list : you are sending a pointer of the queue, which is also call by reference. So I think we don’t have to worry about call by value here 1 1 replyShare Sidd1425 commented Oct 29, 2024 reply Follow flag This exact concept is repeated in GATE 2022 question where elements of Queue are reversed in place.(Using No additional storage) https://gateoverflow.in/371884/gate-cse-2022-question-52 1 1 replyShare Sachin Mittal 1 commented Oct 10, 2025 reply Follow flag \[ \begin{array}{l} \textcolor{blue}{\text{Procedure}}~ f(Q): \\ \quad \vert\ \textcolor{blue}{\text{if}}~ Q ~\text{is not empty then} \\ \quad \vert\quad \vert\ i \leftarrow \text{delete}(Q) \\ \quad \vert\quad \vert\ f(Q) \\ \quad \vert\quad \vert\ \text{insert } i \text{into } Q \\ \quad \vert\ \textcolor{blue}{\text{end if}} \\ \textcolor{blue}{\text{end procedure}} \end{array} \]\[ \begin{array}{rl} Q &= [1, 2, 3] \\ (\text{front} &= 1,~ \text{rear} = 3) \end{array} \] \[ \begin{array}{rl} \textbf{Call: } & f(Q) \\[4pt] \text{First call:} & i = \text{delete}(Q) \Rightarrow Q = [2, 3] \\ & f(Q) \\ & \boxed{i = 1} \\[4pt] \text{Second call:} & i = \text{delete}(Q) \Rightarrow Q = [3] \\ & f(Q) \\ & \boxed{i = 2} \\[4pt] \text{Third call:} & i = \text{delete}(Q) \Rightarrow Q = [] \\ & f(Q) \\ & \boxed{i = 3} \\[4pt] \text{Fourth call:} & Q \text{ is empty } \Rightarrow \text{returns} \end{array} \]\[ \begin{array}{l} \textbf{Now recursion starts returning:} \\[4pt] \text{From third call: } \text{insert}(Q, 3) \Rightarrow Q = [3] \\[4pt] \text{From second call: } \text{insert}(Q, 2) \Rightarrow Q = [3, 2] \\[4pt] \text{From first call: } \text{insert}(Q, 1) \Rightarrow Q = [3, 2, 1] \\[8pt] \textcolor{green}{\text{Final Queue:}}~ Q = [3, 2, 1] \end{array} \] 6 6 replyShare Please log in or register to add a comment.
Best answer 57 57 votes $insert()$ will inserts the values in reverse order. Correct Answer: $B-$ Reverses the order of the elements in the queue $Q.$ srestha answered Jan 22, 2016 • edited May 8, 2021 by gatecse srestha comment Share Follow See all 6 Comments 6 6 Comments reply Show 3 previous comments srestha commented Jun 21, 2017 reply Follow flag any reference ? I donot think call by reference or call by value will make any problem here. 1 1 replyShare akb1115 commented Jun 22, 2017 reply Follow flag 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??? 0 0 replyShare commenter commenter commented Oct 24, 2019 reply Follow flag 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. 0 0 replyShare Please log in or register to add a comment.
13 13 votes Here is traced out recursive tree. Answer: Option B Ashmita answered Apr 11, 2021 Ashmita comment Share Follow 0 reply Please log in or register to add a comment.
10 10 votes answer will be b. 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 sourav. answered Jul 12, 2015 sourav. comment Share Follow See all 4 Comments 4 4 Comments reply Wanted commented Jan 8, 2017 reply Follow flag is ny other better approach to understand it ? 0 0 replyShare nikunj commented Aug 28, 2017 reply Follow flag why it is saved... it is not declared as static and default everything is in activation record please explain this point . 0 0 replyShare Anand Mundhe commented Feb 18, 2018 reply Follow flag Call by value..... –1 –1 replyShare M K harsh commented Oct 21, 2020 reply Follow flag But it is clearly mentioned that insert(Q,i) — inserts the integer i at the rear of the queue. Hence we have to insert the elements at rear , like 5 will be inserted at rear , then 4 , then 3 , then 2 then 1 Hence correct option should be option A . But it is also a fact that , in the question , it is no where mentioned that how rear will be decreamented after each function ends . 0 0 replyShare Please log in or register to add a comment.
6 6 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 Rishi yadav answered Oct 4, 2017 Rishi yadav comment Share Follow See 1 comment 1 1 comment reply Anand Mundhe commented Feb 18, 2018 reply Follow flag Only happens if call by reference..... can u see & anywhere? 0 0 replyShare Please log in or register to add a comment.
3 3 votes ans b) Aditi Dan answered Dec 19, 2014 1 flag: ✌ Spam (Yash_Vaghasiya 369) Aditi Dan comment Share Follow See 1 comment 1 1 comment reply Anand Mundhe commented Feb 18, 2018 reply Follow flag Wrong –2 –2 replyShare Please log in or register to add a comment.
0 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. sutanay3 answered Jul 19, 2018 sutanay3 comment Share Follow 0 reply Please log in or register to add a comment.