Implement a Queue using two Stacks

This problem asks for a efficient way to implement a Queue using two Stacks.

Problem

Implement a Queue using two Stacks
Input:
enqueue(1)
enqueue(2)
enqueue(3)
Output:
1
2
3

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
class Queue:
    def __init__(self):
        self.stack1 = []
        self.stack2 = []

    def enqueue(self, item):
        self.stack1.append(item)

    def dequeue(self):
        if not self.stack2:
            while self.stack1:
                self.stack2.append(self.stack1.pop())
        return self.stack2.pop()

q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
print(q.dequeue())
print(q.dequeue())
print(q.dequeue())

A.I. Evaluation of the Solution

The candidate's solution is correct and solves the problem. The candidate's approach is to use two stacks, with one stack being used for enqueueing and the other stack being used for dequeueing. This is a valid approach.

Evaluated at: 2022-11-10 00:15:50