Queue · Easy
282. Reverse a Queue Using Recursion
Topics: Queue, Recursion
Solve it on DeVert Campus - free code runner
Problem
Given a queue of n integers (front to back), reverse the entire queue using only recursive queue operations (enqueue/dequeue/front/isEmpty, no other data structure - not even an explicit loop-based auxiliary array). Print the resulting queue, front to back.
Input and output
Input: The first line contains an integer n. The second line contains n space-separated integers, the queue's values from front to back.
Output: Print the reversed queue's values, space-separated, front to back.
Example
Input: 5 1 2 3 4 5 Output: 5 4 3 2 1
Constraints
1 <= n <= 10^4
Key observation
The exact ORDER of operations matters enormously here: you must recurse BEFORE re-enqueuing the current element, not after - reversing that order would leave the queue completely unchanged instead of reversed.
Intuition
Reverse an entire queue's order, but using ONLY the queue's own basic operations and recursion - no separate array or extra data structure allowed.
Brute force: Dequeue everything into a plain array or stack, reverse it there, and rebuild the queue - simple and effective, but explicitly disallowed by this problem's own constraint of using ONLY recursive queue operations, no auxiliary data structure.
Optimised: The trick is DELAYING re-insertion: by recursively handling everything BEHIND the current front element before re-adding that front element, you guarantee it gets added back only after everything that should now come before it (in the reversed order) is already in place - the recursion's call stack itself is doing the 'holding' work an explicit stack normally would.
Optimal - Pure Recursion (No Auxiliary Structure)
Recursively dequeue the front element, recurse on the now one-shorter remaining queue to reverse it first, and only after that recursive call returns, enqueue the saved front element onto the back. Because each element is enqueued back only after everything that was behind it has already been fully reversed and placed, the recursion naturally produces the fully reversed queue using only the call stack itself as implicit storage, satisfying the constraint of using no data structure but the queue and recursion.
Time: O(n)
Space: O(n) (call stack only, no explicit auxiliary structure)
Better - Recursion with an Auxiliary Array
Recursively dequeue elements from the queue, and as each recursive call returns (on the way back up the call stack), record each dequeued value into a fixed-position array so the recursion's unwinding order captures the reverse sequence, then enqueue the array's contents back into the queue in that captured order. This is a stepping stone between the brute-force stack approach and a fully recursion-only solution - it uses the call stack to reorder elements like the optimal approach will, but still relies on an explicit array to hold values in between.
Time: O(n)
Space: O(n)
Brute Force - Auxiliary Stack
Dequeue every element from the front of the queue and push each one onto a stack; because a stack is LIFO, popping everything off the stack and enqueuing it back into the queue naturally reverses the original order. This is the most intuitive first approach most people reach for and is easy to reason about, but it relies on an explicit auxiliary stack data structure, which the intended solution for this problem specifically disallows.
Time: O(n)
Space: O(n)
Step by step
1. If the queue is empty, there's nothing to do - stop (the base case).
2. Otherwise, dequeue the front element and set it aside, remembering it.
3. Recursively reverse whatever remains in the queue FIRST, before doing anything else.
4. Only after that recursive call fully finishes, enqueue the element you set aside - this places it AFTER everything that used to be behind it, exactly achieving the reversal.
Complexity
Time: O(n) - each element is dequeued and enqueued exactly once across the whole recursive process.
Space: O(n), for the recursive call stack - one stack frame is active for each element still waiting to be re-inserted, functioning as an implicit stack even though no explicit one was created.
Edge cases
n = 1 (a single-element queue, unchanged by reversal); n = 10^4, the upper bound, given that only recursive queue operations (no auxiliary array) are allowed.
Interview tip
Be ready to explain that this problem is really 'reverse a stack using recursion' in disguise, one level up - the call stack itself substitutes for the auxiliary data structure the problem statement explicitly disallows using directly.