Bird
Raised Fist0
DSA Goprogramming~20 mins

Enqueue Operation in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Enqueue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Enqueue in Circular Queue
What is the printed state of the circular queue after the following enqueue operations?
DSA Go
package main

import "fmt"

type CircularQueue struct {
	items []int
	front int
	rear  int
	size  int
}

func NewCircularQueue(capacity int) *CircularQueue {
	return &CircularQueue{
		items: make([]int, capacity),
		front: 0,
		rear:  -1,
		size:  0,
	}
}

func (q *CircularQueue) Enqueue(val int) bool {
	if q.size == len(q.items) {
		return false
	}
	q.rear = (q.rear + 1) % len(q.items)
	q.items[q.rear] = val
	q.size++
	return true
}

func (q *CircularQueue) String() string {
	result := ""
	for i := 0; i < q.size; i++ {
		index := (q.front + i) % len(q.items)
		result += fmt.Sprintf("%d -> ", q.items[index])
	}
	return result + "null"
}

func main() {
	q := NewCircularQueue(4)
	q.Enqueue(10)
	q.Enqueue(20)
	q.Enqueue(30)
	q.Enqueue(40)
	fmt.Println(q.String())
}
A20 -> 30 -> 40 -> null
B10 -> 20 -> 30 -> null
C10 -> 20 -> 30 -> 40 -> null
Dnull
Attempts:
2 left
💡 Hint
The queue has capacity 4 and can hold exactly 4 elements.
❓ Predict Output
intermediate
2:00remaining
Result of Enqueue on Linked List Queue
What is the printed state of the queue after these enqueue operations on a linked list implementation?
DSA Go
package main

import "fmt"

type Node struct {
	value int
	next  *Node
}

type Queue struct {
	front *Node
	rear  *Node
}

func (q *Queue) Enqueue(val int) {
	node := &Node{value: val}
	if q.rear == nil {
		q.front = node
		q.rear = node
		return
	}
	q.rear.next = node
	q.rear = node
}

func (q *Queue) String() string {
	result := ""
	current := q.front
	for current != nil {
		result += fmt.Sprintf("%d -> ", current.value)
		current = current.next
	}
	return result + "null"
}

func main() {
	q := &Queue{}
	q.Enqueue(5)
	q.Enqueue(15)
	q.Enqueue(25)
	fmt.Println(q.String())
}
A5 -> 15 -> 25 -> null
B25 -> 15 -> 5 -> null
C5 -> 15 -> null
Dnull
Attempts:
2 left
💡 Hint
Enqueue adds to the rear, so order is preserved.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Enqueue Method
What error will occur when running this enqueue method on a queue implemented with a slice?
DSA Go
func Enqueue(queue []int, val int, capacity int) []int {
	if len(queue) >= capacity {
		return queue
	}
	queue = append(queue, val)
	return queue
}
AIndex out of range error
BCompilation error due to missing return
CInfinite loop error
DNo error, but queue can exceed capacity
Attempts:
2 left
💡 Hint
Check the condition that limits queue size.
❓ Predict Output
advanced
2:00remaining
Output After Multiple Enqueues and Wrap Around in Circular Queue
Given this circular queue code, what is the printed queue after these operations?
DSA Go
package main

import "fmt"

type CircularQueue struct {
	items []int
	front int
	rear  int
	size  int
}

func NewCircularQueue(capacity int) *CircularQueue {
	return &CircularQueue{
		items: make([]int, capacity),
		front: 0,
		rear:  -1,
		size:  0,
	}
}

func (q *CircularQueue) Enqueue(val int) bool {
	if q.size == len(q.items) {
		return false
	}
	q.rear = (q.rear + 1) % len(q.items)
	q.items[q.rear] = val
	q.size++
	return true
}

func (q *CircularQueue) Dequeue() (int, bool) {
	if q.size == 0 {
		return 0, false
	}
	val := q.items[q.front]
	q.front = (q.front + 1) % len(q.items)
	q.size--
	return val, true
}

func (q *CircularQueue) String() string {
	result := ""
	for i := 0; i < q.size; i++ {
		index := (q.front + i) % len(q.items)
		result += fmt.Sprintf("%d -> ", q.items[index])
	}
	return result + "null"
}

func main() {
	q := NewCircularQueue(3)
	q.Enqueue(1)
	q.Enqueue(2)
	q.Enqueue(3)
	q.Dequeue()
	q.Enqueue(4)
	fmt.Println(q.String())
}
A4 -> null
B2 -> 3 -> 4 -> null
C3 -> 4 -> null
D1 -> 2 -> 3 -> null
Attempts:
2 left
💡 Hint
Dequeue removes front, enqueue wraps rear index.
🧠 Conceptual
expert
2:00remaining
Maximum Number of Enqueue Operations Before Full in Circular Queue
Consider a circular queue of capacity N. Starting empty, how many successful enqueue operations can be performed before the queue is full?
AN - 1
BN
CN + 1
DN / 2
Attempts:
2 left
💡 Hint
Circular queues often reserve one slot to distinguish full vs empty.

Practice

(1/5)
1. What does the enqueue operation do in a queue data structure?
easy
A. Removes an element from the front of the queue
B. Checks if the queue is empty
C. Adds an element to the end of the queue
D. Returns the size of the queue

Solution

  1. Step 1: Understand the queue structure

    A queue works like a line where new elements join at the end.
  2. Step 2: Define enqueue operation

    Enqueue means adding a new element to the back of the queue.
  3. Final Answer:

    Adds an element to the end of the queue -> Option C
  4. Quick Check:

    Enqueue = Add at end [OK]
Hint: Enqueue means add at the back of the queue [OK]
Common Mistakes:
  • Confusing enqueue with dequeue
  • Thinking enqueue removes elements
  • Mixing enqueue with checking size
2. Which of the following is the correct syntax to enqueue an element 5 into a queue named q in Go?
easy
A. q.Enqueue(5)
B. q.enqueue(5)
C. enqueue(q, 5)
D. q.add(5)

Solution

  1. Step 1: Identify method naming conventions in Go

    Methods in Go are usually capitalized if exported, so Enqueue is correct.
  2. Step 2: Check method call on queue instance

    Calling q.Enqueue(5) means calling method Enqueue on queue q with argument 5.
  3. Final Answer:

    q.Enqueue(5) -> Option A
  4. Quick Check:

    Method call with capital Enqueue = q.Enqueue(5) [OK]
Hint: Use capitalized method names for exported functions in Go [OK]
Common Mistakes:
  • Using lowercase method names incorrectly
  • Calling enqueue as a standalone function
  • Using wrong method name like add
3. What will be the output of the following Go code snippet?
package main

import "fmt"

type Queue struct {
    items []int
}

func (q *Queue) Enqueue(val int) {
    q.items = append(q.items, val)
}

func (q *Queue) Print() {
    for _, v := range q.items {
        fmt.Print(v, " -> ")
    }
    fmt.Println("null")
}

func main() {
    q := Queue{}
    q.Enqueue(10)
    q.Enqueue(20)
    q.Enqueue(30)
    q.Print()
}
medium
A. Compilation error
B. 10 -> 20 -> 30 -> null
C. null
D. 30 -> 20 -> 10 -> null

Solution

  1. Step 1: Understand enqueue implementation

    Enqueue appends new values at the end of the slice, so order is preserved as inserted.
  2. Step 2: Trace the Print method output

    Print prints all items in order separated by ' -> ' and ends with 'null'.
  3. Final Answer:

    10 -> 20 -> 30 -> null -> Option B
  4. Quick Check:

    Enqueue appends, so output is 10 -> 20 -> 30 -> null [OK]
Hint: Enqueue appends at end, print shows order inserted [OK]
Common Mistakes:
  • Thinking enqueue adds at front
  • Confusing print order
  • Assuming code has errors
4. Identify the error in the following Go code for enqueue operation:
package main

import "fmt"

type Queue struct {
    items []int
}

func (q Queue) Enqueue(val int) {
    q.items = append(q.items, val)
}

func main() {
    q := Queue{}
    q.Enqueue(5)
    fmt.Println(q.items)
}
medium
A. Enqueue method should have pointer receiver to modify queue
B. items slice should be initialized with make
C. fmt.Println is not imported
D. Append cannot be used on slices

Solution

  1. Step 1: Check method receiver type

    The Enqueue method uses a value receiver, so changes to q.items do not affect original queue.
  2. Step 2: Understand pointer receiver necessity

    To modify the original queue, Enqueue must have pointer receiver (q *Queue).
  3. Final Answer:

    Enqueue method should have pointer receiver to modify queue -> Option A
  4. Quick Check:

    Pointer receiver needed to modify struct [OK]
Hint: Use pointer receiver to modify struct fields [OK]
Common Mistakes:
  • Using value receiver and expecting changes
  • Forgetting to import fmt (not main error here)
  • Thinking append can't be used on slices
5. You want to implement a queue using a linked list in Go. Which step is essential in the Enqueue operation to maintain the queue correctly?
hard
A. Add new node at the head and do not update head pointer
B. Add new node at the head and update tail pointer
C. Add new node at the tail but do not update tail pointer
D. Add new node at the tail and update tail pointer

Solution

  1. Step 1: Understand linked list queue structure

    Queue adds new elements at the tail (end) and removes from head (front).
  2. Step 2: Enqueue operation details

    To enqueue, add new node at tail and update tail pointer to new node to keep track.
  3. Final Answer:

    Add new node at the tail and update tail pointer -> Option D
  4. Quick Check:

    Enqueue adds at tail and updates tail pointer [OK]
Hint: Enqueue adds at tail and updates tail pointer [OK]
Common Mistakes:
  • Adding at head instead of tail
  • Not updating tail pointer after enqueue
  • Confusing head and tail roles
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