GATE CS 2016 Set 1 — Question 51
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.NAT+2 / -0HardStack OperationsStacks & QueuesProgramming & Data StructuresQueue & Circular Queue
Programming & Data Structures → Stacks & Queues → Queue & Circular Queue
Last updated
Question
Let denote a queue containing sixteen numbers and be an empty stack. returns the element at the head of the queue without removing it from . Similarly returns the element at the top of without removing it from . Consider the algorithm given below.The maximum possible number of iterations of the while loop in the algorithm is __________.
while Q is not Empty do
if S is Empty OR Top(S) <= Head(Q) then
x := Dequeue(Q);
Push(S, x);
else
x := Pop(S);
Enqueue(Q, x);
end
end
Correct answer
256 to 256
Solution
Let be the number of elements in the queue. The algorithm attempts to move elements from the queue to the stack such that the elements in are in non-decreasing order from top to bottom (which means they will be sorted when popped). If the current element at the head of is greater than or equal to the top of , it is pushed onto . If it is smaller, the top element of is popped and enqueued back to . This behavior is similar to a sorting process where elements are cycled until they can be placed in the correct order.The worst-case scenario for the number of iterations occurs when the input queue is sorted in decreasing order (e.g., ). In this case, the algorithm performs iterations.Let's trace for small :
2. : Pop 2, Enqueue 2 ()
3. Dequeue 1, Push 1 ()
4. : Dequeue 2, Push 2 ()
Total: 4 iterations ().
Given , the maximum number of iterations is:
- For (Input: ): 1 iteration.
- For (Input: ):
2. : Pop 2, Enqueue 2 ()
3. Dequeue 1, Push 1 ()
4. : Dequeue 2, Push 2 ()
Total: 4 iterations ().
- For (Input: ): Total 9 iterations ().
Given , the maximum number of iterations is:
Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on Stacks & Queues
2026 Set 2 Q12The set T represents various traversals over binary tree. The set S represents the order of…2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q23Let be an odd number greater than 100. Consider a binary minheap with elements stored in an…2026 Set 1 Q24Consider a hash table that is initially empty. The hash table is maintained…2026 Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]