Bird
Raised Fist0
DSA Goprogramming~20 mins

Enqueue Using Linked List in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Enqueue Operations on Linked List Queue
What is the printed state of the queue after enqueuing 10, 20, and 30 in order?
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}
    if q.rear == nil {
        q.front = newNode
        q.rear = newNode
        return
    }
    q.rear.next = newNode
    q.rear = newNode
}

func (q *Queue) Print() {
    current := q.front
    for current != nil {
        fmt.Print(current.value)
        if current.next != nil {
            fmt.Print(" -> ")
        }
        current = current.next
    }
    fmt.Println(" -> null")
}

func main() {
    q := &Queue{}
    q.Enqueue(10)
    q.Enqueue(20)
    q.Enqueue(30)
    q.Print()
}
A10 -> 20 -> 30 -> null
B30 -> 20 -> 10 -> null
C10 -> 30 -> 20 -> null
Dnull
Attempts:
2 left
💡 Hint
Remember, enqueue adds elements at the rear of the queue.
❓ Predict Output
intermediate
2:00remaining
Result of Enqueue on Empty Queue
What will be the printed output after enqueuing a single element 5 into an empty linked list queue?
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}
    if q.rear == nil {
        q.front = newNode
        q.rear = newNode
        return
    }
    q.rear.next = newNode
    q.rear = newNode
}

func (q *Queue) Print() {
    current := q.front
    for current != nil {
        fmt.Print(current.value)
        if current.next != nil {
            fmt.Print(" -> ")
        }
        current = current.next
    }
    fmt.Println(" -> null")
}

func main() {
    q := &Queue{}
    q.Enqueue(5)
    q.Print()
}
A0 -> null
Bnull
C5 -> null
D5
Attempts:
2 left
💡 Hint
Check how enqueue handles the empty queue case.
🔧 Debug
advanced
2:00remaining
Identify the Error in Enqueue Implementation
What error will occur when running this enqueue code on an empty queue?
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}
    if q.front == nil {
        q.front = newNode
        // Missing assignment to rear
        return
    }
    q.rear.next = newNode
    q.rear = newNode
}

func (q *Queue) Print() {
    current := q.front
    for current != nil {
        fmt.Print(current.value)
        if current.next != nil {
            fmt.Print(" -> ")
        }
        current = current.next
    }
    fmt.Println(" -> null")
}

func main() {
    q := &Queue{}
    q.Enqueue(1)
    q.Enqueue(2)
    q.Print()
}
AOutput: 1 -> 2 -> null
Bruntime error: invalid memory address or nil pointer dereference
CCompilation error: missing return statement
DOutput: 2 -> 1 -> null
Attempts:
2 left
💡 Hint
Check what happens to q.rear when queue is empty.
🧠 Conceptual
advanced
2:00remaining
Why Use Rear Pointer in Linked List Queue?
Why is it important to maintain a rear pointer in a linked list implementation of a queue?
ATo allow dequeue operation to remove elements from the rear
BTo reverse the queue efficiently
CTo keep track of the number of elements in the queue
DTo allow enqueue operation to add elements in O(1) time at the end
Attempts:
2 left
💡 Hint
Think about how adding elements at the end works in a linked list.
❓ Predict Output
expert
3:00remaining
Final Queue State After Mixed Enqueue Operations
Given the following enqueue operations on an initially empty queue, what is the final printed queue state?
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}
    if q.rear == nil {
        q.front = newNode
        q.rear = newNode
        return
    }
    q.rear.next = newNode
    q.rear = newNode
}

func (q *Queue) Print() {
    current := q.front
    for current != nil {
        fmt.Print(current.value)
        if current.next != nil {
            fmt.Print(" -> ")
        }
        current = current.next
    }
    fmt.Println(" -> null")
}

func main() {
    q := &Queue{}
    q.Enqueue(7)
    q.Enqueue(14)
    q.Enqueue(21)
    q.Enqueue(28)
    q.Print()
}
A7 -> 14 -> 21 -> 28 -> null
B28 -> 21 -> 14 -> 7 -> null
C7 -> 21 -> 14 -> 28 -> null
Dnull
Attempts:
2 left
💡 Hint
Enqueue adds elements at the rear in order.

Practice

(1/5)
1. What does the enqueue operation do in a queue implemented using a linked list?
easy
A. Deletes the last element from the linked list
B. Adds a new element at the end of the linked list
C. Removes the first element from the linked list
D. Checks if the linked list is empty

Solution

  1. Step 1: Understand enqueue operation

    Enqueue means adding an element to the queue.
  2. Step 2: Enqueue in linked list context

    In a linked list queue, enqueue adds the new node at the end (tail).
  3. Final Answer:

    Adds a new element at the end of the linked list -> Option B
  4. Quick Check:

    Enqueue = Add at end [OK]
Hint: Enqueue always adds at the tail of the linked list [OK]
Common Mistakes:
  • Confusing enqueue with dequeue operation
  • Adding element at the head instead of tail
  • Thinking enqueue removes elements
  • Assuming enqueue checks emptiness
2. Which of the following is the correct syntax to create a new node and enqueue it in Go using a linked list?
easy
A. node := &Node{data: value, next: nil} queue.tail.next = node queue.tail = node
B. node := Node{value, nil} queue.head = node
C. node := &Node{value} queue.head.next = node
D. node := Node{data: value} queue.tail = node.next

Solution

  1. Step 1: Create new node correctly

    Use pointer to Node with data and next set to nil.
  2. Step 2: Link new node at tail and update tail

    Set current tail's next to new node, then update tail pointer.
  3. Final Answer:

    node := &Node{data: value, next: nil} queue.tail.next = node queue.tail = node -> Option A
  4. Quick Check:

    New node pointer + update tail = A [OK]
Hint: Always use pointer and update tail's next and tail itself [OK]
Common Mistakes:
  • Not using pointer for new node
  • Assigning new node to head instead of tail
  • Forgetting to update tail pointer
  • Incorrect struct initialization syntax
3. What will be the state of the queue after executing the following Go code?
type Node struct {
    data int
    next *Node
}

type Queue struct {
    head *Node
    tail *Node
}

func (q *Queue) enqueue(value int) {
    node := &Node{data: value}
    if q.tail == nil {
        q.head = node
        q.tail = node
    } else {
        q.tail.next = node
        q.tail = node
    }
}

func main() {
    q := &Queue{}
    q.enqueue(10)
    q.enqueue(20)
    q.enqueue(30)
    // Print queue
    current := q.head
    for current != nil {
        fmt.Print(current.data, " -> ")
        current = current.next
    }
    fmt.Print("null")
}
medium
A. 30 -> 20 -> 10 -> null
B. null
C. 10 -> 30 -> 20 -> null
D. 10 -> 20 -> 30 -> null

Solution

  1. Step 1: Enqueue elements in order

    Elements 10, 20, 30 are added at the tail one by one.
  2. Step 2: Traverse from head to tail

    Traversal prints nodes in order: 10, then 20, then 30, then null.
  3. Final Answer:

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

    Enqueue order preserved in linked list [OK]
Hint: Enqueue adds at tail, print from head to tail [OK]
Common Mistakes:
  • Assuming new elements added at head
  • Printing tail to head order
  • Forgetting to print 'null' at end
  • Confusing enqueue with dequeue output
4. Identify the error in the following Go enqueue function for a linked list queue and select the fix.
func (q *Queue) enqueue(value int) {
    node := &Node{data: value}
    if q.head == nil {
        q.head = node
    } else {
        q.tail.next = node
    }
}
medium
A. Missing update of q.tail to new node after adding
B. Should check q.tail == nil instead of q.head == nil
C. Node should be created without pointer
D. Should assign q.head.next = node instead of q.tail.next

Solution

  1. Step 1: Analyze enqueue logic

    Function sets head if empty, else links new node after tail.
  2. Step 2: Check tail pointer update

    Function does not update q.tail to new node, so tail remains old node.
  3. Final Answer:

    Missing update of q.tail to new node after adding -> Option A
  4. Quick Check:

    Update tail pointer after enqueue [OK]
Hint: Always update tail pointer after adding new node [OK]
Common Mistakes:
  • Not updating tail pointer after enqueue
  • Checking wrong pointer for empty queue
  • Creating node without pointer
  • Linking new node to head instead of tail
5. You want to implement a queue using a linked list in Go that supports enqueue and dequeue efficiently. Which of the following changes will improve the enqueue operation's time complexity from O(n) to O(1)?
hard
A. Add new nodes at the head instead of the tail
B. Use a doubly linked list instead of singly linked list
C. Maintain a tail pointer to the last node and update it on enqueue
D. Traverse the list to find the last node each time before enqueue

Solution

  1. Step 1: Understand enqueue time complexity

    Without tail pointer, enqueue requires traversal to last node, O(n).
  2. Step 2: Using tail pointer

    Maintaining tail pointer allows direct access to last node, making enqueue O(1).
  3. Final Answer:

    Maintain a tail pointer to the last node and update it on enqueue -> Option C
  4. Quick Check:

    Tail pointer reduces enqueue to O(1) [OK]
Hint: Keep tail pointer to avoid traversal on enqueue [OK]
Common Mistakes:
  • Thinking doubly linked list alone improves enqueue
  • Adding at head breaks queue order
  • Traversing list each time causes O(n) complexity
  • Ignoring tail pointer update
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