0
0
DSA Pythonprogramming~20 mins

Implement Stack Using Queue in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stack Using Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Push and Pop Operations on Stack Implemented Using One Queue
What is the output of the following code that uses one queue to implement a stack?
DSA Python
from collections import deque

class StackUsingQueue:
    def __init__(self):
        self.q = deque()

    def push(self, x):
        self.q.append(x)
        for _ in range(len(self.q) - 1):
            self.q.append(self.q.popleft())

    def pop(self):
        if self.q:
            return self.q.popleft()
        return None

stack = StackUsingQueue()
stack.push(10)
stack.push(20)
stack.push(30)
print(stack.pop())
print(stack.pop())
print(stack.pop())
A30\n10\n20
B10\n20\n30
C30\n20\n10
D20\n30\n10
Attempts:
2 left
💡 Hint
Think about how the queue is rotated after each push to simulate stack behavior.
🧠 Conceptual
intermediate
1:30remaining
Understanding Time Complexity of Stack Operations Using Two Queues
If a stack is implemented using two queues where push operation is costly and pop operation is cheap, what is the time complexity of push and pop respectively?
APush: O(n), Pop: O(1)
BPush: O(n), Pop: O(n)
CPush: O(1), Pop: O(n)
DPush: O(1), Pop: O(1)
Attempts:
2 left
💡 Hint
Consider which operation involves moving all elements between queues.
🔧 Debug
advanced
1:30remaining
Identify the Error in Stack Using Queue Implementation
What error will the following code produce when calling pop on an empty stack implemented using one queue?
DSA Python
from collections import deque

class StackUsingQueue:
    def __init__(self):
        self.q = deque()

    def push(self, x):
        self.q.append(x)
        for _ in range(len(self.q) - 1):
            self.q.append(self.q.popleft())

    def pop(self):
        return self.q.popleft()

stack = StackUsingQueue()
print(stack.pop())
AAttributeError
BIndexError: pop from an empty deque
CNone
DIndexError
Attempts:
2 left
💡 Hint
What happens when you pop from an empty deque?
Predict Output
advanced
2:00remaining
Output of Mixed Push and Pop Operations Using Two Queues
What is the output of the following code implementing stack with two queues where pop is costly?
DSA Python
from collections import deque

class StackUsingTwoQueues:
    def __init__(self):
        self.q1 = deque()
        self.q2 = deque()

    def push(self, x):
        self.q1.append(x)

    def pop(self):
        if not self.q1:
            return None
        while len(self.q1) > 1:
            self.q2.append(self.q1.popleft())
        res = self.q1.popleft()
        self.q1, self.q2 = self.q2, self.q1
        return res

stack = StackUsingTwoQueues()
stack.push(5)
stack.push(10)
print(stack.pop())
stack.push(15)
print(stack.pop())
print(stack.pop())
A10\n15\n5
B5\n10\n15
C15\n10\n5
D10\n5\n15
Attempts:
2 left
💡 Hint
Pop removes the last pushed element by moving all but last to the other queue.
🧠 Conceptual
expert
2:30remaining
Minimum Number of Queues to Implement a Stack with O(1) Amortized Push and Pop
What is the minimum number of standard FIFO queues required to implement a stack with both push and pop operations having O(1) amortized time complexity?
ATwo queues
BOne queue
CThree queues
DImpossible with any finite number of queues
Attempts:
2 left
💡 Hint
Consider the fundamental difference between FIFO and LIFO behaviors and the cost of rearranging elements.