Here is the interview question prompt, presented for reference.
Can you implement a queue using two stacks?
Many languages have queues modeled using arrays or lists, so we can mimic this constraint by only allowing those data structures to
enqueue to the end and
dequeue from the start.
The following method should work.
const tsq = new TwoStackQueue(); tsq.push(1); tsq.push(2); tsq.pop(); // 1 tsq.pop(); // 2
O(1)if pop operation is
O(1)if push operation is
You can see the full challenge with visuals at this link.
Challenges • Asked over 3 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Two Stack Queue.
Test case 3 is wrong
For which language?
According to test 3
tsq = TwoStackQueue(); tsq.push(1); tsq.push(2); tsq.pop(); tsq.push(3); tsq.pop(); assert tsq.pop() == 1
tsq should pop 1 but it pop 3 since the operation order is push,push,pop,push,pop,pop