0
0
DSA Pythonprogramming~10 mins

Queue Using Two Stacks 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 add an element to the queue using stack push operation.

DSA Python
def enqueue(self, x):
    self.stack_in.[1](x)
Drag options to blanks, or click blank then click option'
Ainsert
Bpush
Cpop
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of append causes removal instead of addition.
Using push is incorrect because Python lists do not have push method.
2fill in blank
medium

Complete the code to check if the queue is empty by checking both stacks.

DSA Python
def is_empty(self):
    return len(self.stack_in) == 0 and len(self.stack_out) [1] 0
Drag options to blanks, or click blank then click option'
A==
B!=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == causes wrong empty check.
Using > or < does not correctly check for emptiness.
3fill in blank
hard

Fix the error in the dequeue method to correctly transfer elements from stack_in to stack_out.

DSA Python
def dequeue(self):
    if not self.stack_out:
        while self.stack_in:
            self.stack_out.[1](self.stack_in.pop())
    return self.stack_out.pop()
Drag options to blanks, or click blank then click option'
Aappend
Bpop
Cpush
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of append causes removal instead of addition.
Using push is invalid for Python lists.
4fill in blank
hard

Fill both blanks to complete the peek method that returns the front element without removing it.

DSA Python
def peek(self):
    if not self.stack_out:
        while self.stack_in:
            self.stack_out.[1](self.stack_in.pop())
    return self.stack_out[[2]]
Drag options to blanks, or click blank then click option'
Aappend
Bpop
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of append causes errors.
Using index 0 returns the most recently added element, not the front.
5fill in blank
hard

Fill all three blanks to complete the full Queue class using two stacks.

DSA Python
class QueueUsingTwoStacks:
    def __init__(self):
        self.stack_in = []
        self.stack_out = []

    def enqueue(self, x):
        self.stack_in.[1](x)

    def dequeue(self):
        if not self.stack_out:
            while self.stack_in:
                self.stack_out.[2](self.stack_in.pop())
        return self.stack_out.pop()

    def is_empty(self):
        return len(self.stack_in) == 0 and len(self.stack_out) [3] 0
Drag options to blanks, or click blank then click option'
Aappend
Bpop
C==
Dpush
Attempts:
3 left
💡 Hint
Common Mistakes
Using push instead of append causes errors.
Using pop to add elements is incorrect.
Using != instead of == in is_empty causes wrong results.