Bird
Raised Fist0
DSA Goprogramming

Enqueue Operation in DSA Go

Choose your learning style10 modes available
Mental Model
Add a new item to the end of a line so it waits its turn. The line grows by one at the back.
Analogy: Imagine people standing in a queue at a ticket counter. When a new person arrives, they join at the end of the line, not in the middle or front.
front -> 10 -> 20 -> 30 -> null
rear ↑
Dry Run Walkthrough
Input: queue: 10 -> 20 -> 30 -> null, enqueue value 40
Goal: Add 40 to the end of the queue so it becomes the new last item
Step 1: Create a new node with value 40 and next pointer null
newNode(40) -> null
Why: We need a new node to add to the queue
Step 2: Link current rear node (30) next pointer to new node (40)
10 -> 20 -> 30 -> [next->40] -> null
Why: Attach new node at the end of the queue
Step 3: Update rear pointer to point to new node (40)
front -> 10 -> 20 -> 30 -> 40 -> null
rear ↑
Why: Rear must always point to the last node
Result:
front -> 10 -> 20 -> 30 -> 40 -> null
rear ↑
Annotated Code
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) {
	newNode := &Node{value: val, next: nil} // create new node
	if q.rear == nil {                      // queue empty
		q.front = newNode                     // front and rear both point to new node
		q.rear = newNode
		return
	}
	q.rear.next = newNode                   // link new node at end
	q.rear = newNode                        // update rear pointer
}

func (q *Queue) Print() {
	curr := q.front
	for curr != nil {
		fmt.Printf("%d -> ", curr.value)
		curr = curr.next
	}
	fmt.Println("null")
}

func main() {
	q := &Queue{}
	q.Enqueue(10)
	q.Enqueue(20)
	q.Enqueue(30)
	q.Print() // before enqueue 40
	q.Enqueue(40)
	q.Print() // after enqueue 40
}
newNode := &Node{value: val, next: nil} // create new node
create a new node with the given value and no next node
if q.rear == nil {
check if queue is empty to initialize front and rear
q.rear.next = newNode
link the new node at the end of the queue
q.rear = newNode
update rear pointer to new last node
OutputSuccess
10 -> 20 -> 30 -> null 10 -> 20 -> 30 -> 40 -> null
Complexity Analysis
Time: O(1) because we add the new node directly at the rear without traversing
Space: O(1) because only one new node is created per enqueue
vs Alternative: Compared to using an array where adding at the end might require resizing or shifting, linked list enqueue is faster and constant time
Edge Cases
empty queue
front and rear both point to the new node
DSA Go
if q.rear == nil {
When to Use This Pattern
When you see a problem asking to add items in order and process them in the same order, reach for the enqueue operation in a queue data structure because it adds items at the end efficiently.
Common Mistakes
Mistake: Not updating the rear pointer after adding the new node
Fix: Always set rear = newNode after linking it
Mistake: Not handling the empty queue case where front and rear are nil
Fix: Check if rear is nil and set both front and rear to newNode
Summary
Adds a new element at the end of the queue.
Use when you want to keep order and add items to wait their turn.
Remember to update the rear pointer and handle empty queue separately.

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