0
0
DSA Pythonprogramming~10 mins

Implement Stack Using Queue in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the queue in the stack constructor.

DSA Python
import collections
class StackUsingQueue:
    def __init__(self):
        self.queue = [1]
Drag options to blanks, or click blank then click option'
Alist()
B[]
Ccollections.deque()
Dqueue.Queue()
Attempts:
3 left
💡 Hint
Common Mistakes
Using a plain list which is inefficient for queue operations.
Using queue.Queue() which is for threading and not needed here.
2fill in blank
medium

Complete the code to add an element to the stack using the queue.

DSA Python
def push(self, x):
    self.queue.append(x)
    for _ in range(len(self.queue) - 1):
        self.queue.append([1])
Drag options to blanks, or click blank then click option'
Aself.queue.popitem()
Bself.queue.pop(0)
Cself.queue.pop()
Dself.queue.popleft()
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which removes from the end, not the front.
Using pop(0) on deque which is invalid.
3fill in blank
hard

Fix the error in the pop method to remove and return the top element of the stack.

DSA Python
def pop(self):
    if not self.queue:
        return None
    return self.queue.[1]()
Drag options to blanks, or click blank then click option'
Apopleft
Bpop
Cremove
Ddequeue
Attempts:
3 left
💡 Hint
Common Mistakes
Using popleft() which removes from the front, not the top.
Using remove() which requires a value, not an index.
4fill in blank
hard

Fill both blanks to implement the top method that returns the top element without removing it.

DSA Python
def top(self):
    if not self.queue:
        return None
    return self.queue[1]
Drag options to blanks, or click blank then click option'
A[-1]
B[0]
C.pop()
D.popleft()
Attempts:
3 left
💡 Hint
Common Mistakes
Using [0] which accesses the front, not the top.
Using .pop() which removes the element.
5fill in blank
hard

Fill all three blanks to implement the empty method that checks if the stack is empty.

DSA Python
def empty(self):
    return [1] == [2]
Drag options to blanks, or click blank then click option'
Alen(self.queue)
B0
C[]
Dself.queue
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing the queue object directly to an empty list.
Using self.queue == 0 which is invalid.