Bird
Raised Fist0
DSA Goprogramming~20 mins

Queue Using Two Stacks in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Two Stacks Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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())
}
A4 -> null
B2 -> 3 -> 4 -> null
C1 -> 2 -> 3 -> 4 -> null
D3 -> 4 -> null
Attempts:
2 left
💡 Hint
Remember that dequeue moves elements from stack1 to stack2 only when stack2 is empty.
❓ Predict Output
intermediate
2:00remaining
Result 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())
A
10
20
30
40
B
10
20
40
30
C
10
30
20
40
D
20
10
30
40
Attempts:
2 left
💡 Hint
Dequeue removes elements in the order they were enqueued.
🔧 Debug
advanced
2:00remaining
Identify 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
}
A
func (q *Queue) Dequeue() int {
    n := len(q.stack2)
    x := q.stack2[n-1]
    q.stack2 = q.stack2[:n-1]
    return x
}
B
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
}
C
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]
        }
    }
    if len(q.stack2) == 0 {
        return -1
    }
    n := len(q.stack2)
    x := q.stack2[n-1]
    q.stack2 = q.stack2[:n-1]
    return x
}
D
func (q *Queue) Dequeue() int {
    if len(q.stack1) == 0 && len(q.stack2) == 0 {
        panic("empty queue")
    }
    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.
🧠 Conceptual
advanced
2:00remaining
Time complexity of dequeue operation
What is the amortized time complexity of the Dequeue operation in a queue implemented using two stacks?
AO(log n) amortized time
BO(n) worst case for every dequeue
CO(1) amortized time
DO(n^2) worst case
Attempts:
2 left
💡 Hint
Consider how often elements are moved between stacks.
🚀 Application
expert
3:00remaining
Final 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())
A5 -> 10 -> 15 -> 20 -> 25 -> null
B10 -> 15 -> 20 -> 25 -> null
C20 -> 25 -> null
D15 -> 20 -> 25 -> null
Attempts:
2 left
💡 Hint
Track each enqueue and dequeue carefully to see which elements remain.

Practice

(1/5)
1. What is the main idea behind implementing a Queue Using Two Stacks?
easy
A. Use two stacks to store elements in sorted order
B. Use one stack for enqueue and another for dequeue to reverse order
C. Use two stacks to store elements in a tree structure
D. Use one stack to store even elements and another for odd elements

Solution

  1. Step 1: Understand queue and stack behavior

    A queue follows FIFO (first in, first out), while a stack follows LIFO (last in, first out).
  2. Step 2: Use two stacks to reverse order

    By pushing elements into one stack and popping them into another, the order reverses, simulating queue behavior.
  3. Final Answer:

    Use one stack for enqueue and another for dequeue to reverse order -> Option B
  4. Quick Check:

    Two stacks reverse order to mimic queue = B [OK]
Hint: Remember: two stacks reverse order to simulate queue [OK]
Common Mistakes:
  • Thinking both stacks store elements in sorted order
  • Confusing stack behavior with queue behavior
  • Using stacks to separate even and odd elements
2. Which of the following is the correct syntax to push an element x into a stack named stack1 in Go?
easy
A. stack1 = append(stack1, x)
B. stack1.Push(x)
C. stack1.add(x)
D. stack1.insert(x)

Solution

  1. Step 1: Understand Go slice push operation

    In Go, stacks are often implemented using slices, and pushing means appending an element.
  2. Step 2: Use append to add element

    The correct way to push is using stack1 = append(stack1, x) to add x at the end.
  3. Final Answer:

    stack1 = append(stack1, x) -> Option A
  4. Quick Check:

    Go slice push uses append = A [OK]
Hint: Use append(slice, x) to push in Go stack [OK]
Common Mistakes:
  • Using non-existent Push() method on slice
  • Using add() or insert() which are not Go slice methods
  • Trying to push without reassigning slice
3. Given the following Go code implementing a queue using two stacks, what will be printed?
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) - 1
      q.stack2 = append(q.stack2, q.stack1[n])
      q.stack1 = q.stack1[:n]
    }
  }
  n := len(q.stack2) - 1
  val := q.stack2[n]
  q.stack2 = q.stack2[:n]
  return val
}

func main() {
  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())
}
medium
A. 20 10 30 40
B. 30 20 10 40
C. 10 20 40 30
D. 10 20 30 40

Solution

  1. Step 1: Trace enqueue operations

    Enqueue 10, 20, 30 adds to stack1: [10, 20, 30]
  2. Step 2: Dequeue operations transfer stack1 to stack2 and pop

    First dequeue moves all from stack1 to stack2 reversing order: stack2 = [30, 20, 10]. Pop from stack2 returns 10, then 20. Enqueue 40 adds to stack1: [40]. Next dequeue pops 30 from stack2. Last dequeue pops 40 from stack1 after transfer.
  3. Final Answer:

    10 20 30 40 -> Option D
  4. Quick Check:

    Queue order preserved by two stacks = A [OK]
Hint: Remember stack2 reverses stack1 to dequeue in FIFO order [OK]
Common Mistakes:
  • Assuming stack1 pops directly for dequeue
  • Not transferring all elements from stack1 to stack2
  • Mixing order of elements after transfer
4. Identify the bug in this Go code snippet for dequeue operation in a queue using two stacks:
func (q *Queue) Dequeue() int {
  if len(q.stack2) == 0 {
    for len(q.stack1) > 0 {
      n := len(q.stack1) - 1
      q.stack2 = append(q.stack2, q.stack1[n])
      q.stack1 = q.stack1[:n]
    }
  }
  val := q.stack2[0]
  q.stack2 = q.stack2[1:]
  return val
}
medium
A. Appending elements to stack2 instead of pushing
B. Not checking if both stacks are empty before dequeue
C. Accessing q.stack2[0] instead of last element causes wrong dequeue order
D. Using len(q.stack1) - 1 incorrectly in loop

Solution

  1. Step 1: Understand stack2 usage in dequeue

    stack2 stores elements in reversed order; popping should remove last element (end of slice).
  2. Step 2: Identify incorrect element access

    Accessing q.stack2[0] removes first element, breaking LIFO order of stack2 and queue FIFO behavior.
  3. Final Answer:

    Accessing q.stack2[0] instead of last element causes wrong dequeue order -> Option C
  4. Quick Check:

    Dequeue must pop last stack2 element = C [OK]
Hint: Pop from end of stack slice, not from start [OK]
Common Mistakes:
  • Removing first element of stack2 instead of last
  • Not checking empty stacks before dequeue
  • Misusing slice indices in loop
5. You want to implement a Peek() function for the queue using two stacks in Go that returns the front element without removing it. Which approach correctly returns the front element?
hard
A. If stack2 is empty, transfer all elements from stack1 to stack2, then return last element of stack2
B. Return first element of stack1 if not empty, else last element of stack2
C. Return last element of stack1 always
D. Transfer elements from stack2 to stack1 and return first element of stack1

Solution

  1. Step 1: Understand peek logic in two-stack queue

    Peek must return the front element, which is at the top of stack2 if not empty.
  2. Step 2: Transfer elements if stack2 empty and return last element

    If stack2 is empty, transfer all from stack1 to stack2 to reverse order, then peek last element of stack2 without removing.
  3. Final Answer:

    If stack2 is empty, transfer all elements from stack1 to stack2, then return last element of stack2 -> Option A
  4. Quick Check:

    Peek front = top of stack2 after transfer = D [OK]
Hint: Peek top of stack2 after transfer if empty [OK]
Common Mistakes:
  • Peeking first element of stack1 which is wrong order
  • Always returning last element of stack1 ignoring stack2
  • Transferring elements in wrong direction
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard