Challenge - 5 Problems
Two Stacks Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of enqueue and dequeue operations
What is the printed state of the queue after these operations using two stacks?
DSA Go
package main import "fmt" type Queue struct { stack1 []int stack2 []int } func (q *Queue) Enqueue(x int) { q.stack1 = append(q.stack1, x) } func (q *Queue) Dequeue() int { if len(q.stack2) == 0 { for len(q.stack1) > 0 { n := len(q.stack1) q.stack2 = append(q.stack2, q.stack1[n-1]) q.stack1 = q.stack1[:n-1] } } n := len(q.stack2) x := q.stack2[n-1] q.stack2 = q.stack2[:n-1] return x } func (q *Queue) Print() string { result := []int{} for i := len(q.stack2) - 1; i >= 0; i-- { result = append(result, q.stack2[i]) } for _, v := range q.stack1 { result = append(result, v) } s := "" for i, v := range result { if i > 0 { s += " -> " } s += fmt.Sprint(v) } s += " -> null" return s } func main() { q := Queue{} q.Enqueue(1) q.Enqueue(2) q.Enqueue(3) q.Dequeue() q.Enqueue(4) fmt.Println(q.Print()) }
Attempts:
2 left
💡 Hint
Remember that dequeue moves elements from stack1 to stack2 only when stack2 is empty.
✗ Incorrect
After enqueueing 1,2,3 and dequeuing once, 1 is removed. Then enqueue 4. The queue order is 2,3,4.
❓ Predict Output
intermediateResult of multiple dequeues
What is the output of the following code snippet using queue with two stacks?
DSA Go
q := Queue{}
q.Enqueue(10)
q.Enqueue(20)
q.Enqueue(30)
fmt.Println(q.Dequeue())
fmt.Println(q.Dequeue())
q.Enqueue(40)
fmt.Println(q.Dequeue())
fmt.Println(q.Dequeue())Attempts:
2 left
💡 Hint
Dequeue removes elements in the order they were enqueued.
✗ Incorrect
Dequeue returns 10, then 20, then after enqueue 40, returns 30 and 40 in order.
🔧 Debug
advancedIdentify the error in dequeue implementation
Which option shows the code that will cause a runtime error when calling Dequeue on an empty queue?
DSA Go
func (q *Queue) Dequeue() int { if len(q.stack2) == 0 { for len(q.stack1) > 0 { n := len(q.stack1) q.stack2 = append(q.stack2, q.stack1[n-1]) q.stack1 = q.stack1[:n-1] } } n := len(q.stack2) x := q.stack2[n-1] q.stack2 = q.stack2[:n-1] return x }
Attempts:
2 left
💡 Hint
Check what happens if both stacks are empty before accessing elements.
✗ Incorrect
Option A does not check if stacks are empty before accessing, causing runtime panic on empty queue.
🧠 Conceptual
advancedTime complexity of dequeue operation
What is the amortized time complexity of the Dequeue operation in a queue implemented using two stacks?
Attempts:
2 left
💡 Hint
Consider how often elements are moved between stacks.
✗ Incorrect
Each element is moved at most twice, so average cost per dequeue is constant time.
🚀 Application
expertFinal queue state after mixed operations
Given the following sequence of operations on a queue implemented with two stacks, what is the final printed queue state?
DSA Go
q := Queue{}
q.Enqueue(5)
q.Enqueue(10)
q.Dequeue()
q.Enqueue(15)
q.Enqueue(20)
q.Dequeue()
q.Enqueue(25)
fmt.Println(q.Print())Attempts:
2 left
💡 Hint
Track each enqueue and dequeue carefully to see which elements remain.
✗ Incorrect
Dequeues remove 5 and 10, remaining elements are 15, 20, 25 in order.
