Bird
Raised Fist0
DSA Goprogramming~20 mins

Queue Implementation Using Array in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Queue Mastery Badge
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?
DSA Go
package main

import "fmt"

type Queue struct {
    items []int
    front int
    rear  int
    size  int
    capacity int
}

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

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

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

func (q *Queue) Print() {
    fmt.Print("Queue: ")
    for i := 0; i < q.size; i++ {
        index := (q.front + i) % q.capacity
        fmt.Printf("%d", q.items[index])
        if i != q.size-1 {
            fmt.Print(" -> ")
        }
    }
    fmt.Println(" -> null")
}

func main() {
    q := NewQueue(5)
    q.Enqueue(10)
    q.Enqueue(20)
    q.Enqueue(30)
    q.Dequeue()
    q.Enqueue(40)
    q.Enqueue(50)
    q.Dequeue()
    q.Enqueue(60)
    q.Print()
}
AQueue: 20 -> 30 -> 40 -> 50 -> 60 -> null
BQueue: 10 -> 20 -> 30 -> 40 -> 50 -> null
CQueue: 30 -> 40 -> 50 -> 60 -> null
DQueue: 40 -> 50 -> 60 -> null
Attempts:
2 left
💡 Hint
Track the front and rear pointers carefully after each enqueue and dequeue.
🧠 Conceptual
intermediate
1:30remaining
Understanding Circular Queue Behavior
Why does the queue implementation use modulo (%) operation when updating front and rear indices?
ATo prevent duplicate elements in the queue
BTo wrap around the indices to the start when they reach the end of the array
CTo increase the size of the queue dynamically
DTo sort the queue elements automatically
Attempts:
2 left
💡 Hint
Think about what happens when the rear reaches the last index of the array.
🔧 Debug
advanced
1:30remaining
Identify the Bug in Dequeue Method
What error will occur if the Dequeue method is called on an empty queue?
DSA Go
func (q *Queue) Dequeue() (int, bool) {
    val := q.items[q.front]
    q.front = (q.front + 1) % q.capacity
    q.size--
    return val, true
}
ARuntime error: index out of range
BLogical error: returns wrong value
CCompilation error: missing return statement
DNo error, returns zero value
Attempts:
2 left
💡 Hint
What happens if size is zero and you access q.items[q.front]?
📝 Syntax
advanced
1:00remaining
Correct the Syntax Error in Enqueue Method
Which option fixes the syntax error in this Enqueue method snippet?
DSA Go
func (q *Queue) Enqueue(val int) bool {
    if q.size == q.capacity {
        return false
    }
    q.rear = (q.rear + 1) % q.capacity
    q.items[q.rear] = val
    q.size++
    return true
}
ARemove the line 'q.rear = (q.rear + 1) % q.capacity'
BReplace 'return false' with 'return nil'
CChange 'q.size++' to 'q.size += 2'
DAdd a closing brace '}' at the end of the function
Attempts:
2 left
💡 Hint
Check if all opening braces have matching closing braces.
🚀 Application
expert
1:30remaining
Maximum Number of Items in Circular Queue
Given a circular queue implemented with an array of capacity 7, what is the maximum number of items it can hold without resizing?
A6
B7
C8
D5
Attempts:
2 left
💡 Hint
Circular queues often reserve one slot to distinguish full vs empty states.

Practice

(1/5)
1. What is the main characteristic of a queue implemented using an array?
easy
A. It allows random access to elements
B. It follows Last-In-First-Out (LIFO) order
C. It follows First-In-First-Out (FIFO) order
D. It stores elements in sorted order

Solution

  1. Step 1: Understand queue behavior

    A queue adds elements at the rear and removes from the front, so the first element added is the first removed.
  2. Step 2: Match behavior with options

    This behavior is called First-In-First-Out (FIFO), which matches It follows First-In-First-Out (FIFO) order.
  3. Final Answer:

    It follows First-In-First-Out (FIFO) order -> Option C
  4. Quick Check:

    Queue = FIFO [OK]
Hint: Queue means first in, first out order [OK]
Common Mistakes:
  • Confusing queue with stack (LIFO)
  • Thinking queue allows random access
  • Assuming queue sorts elements automatically
2. Which of the following is the correct way to declare a fixed-size queue array of size 5 in Go?
easy
A. var queue [5]int
B. queue := []int{5}
C. queue := [5]int
D. queue := make([]int, 5)

Solution

  1. Step 1: Understand array declaration in Go

    Fixed-size arrays in Go are declared with var name [size]type, e.g., var queue [5]int.
  2. Step 2: Check options for fixed-size array

    var queue [5]int uses var queue [5]int which is correct fixed-size array syntax.
  3. Final Answer:

    var queue [5]int -> Option A
  4. Quick Check:

    Fixed array = var name [size]type [OK]
Hint: Fixed array uses var name [size]type in Go [OK]
Common Mistakes:
  • Using make for fixed-size array (make is for slices)
  • Using slice literal with size inside braces
  • Confusing slice and array syntax
3. Consider this Go code snippet implementing a queue using an array and two indices front and rear:
var queue [5]int
front, rear := -1, -1

// Enqueue 10
rear++
queue[rear] = 10

// Enqueue 20
rear++
queue[rear] = 20

// Dequeue
front++

// Enqueue 30
rear++
queue[rear] = 30

// Current queue elements from front+1 to rear are printed
What will be the printed queue elements?
medium
A. [20, 30]
B. [10, 20, 30]
C. [10, 30]
D. [30]

Solution

  1. Step 1: Trace enqueue and dequeue operations

    Initially front = -1, rear = -1. Enqueue 10: rear=0, queue[0]=10 Enqueue 20: rear=1, queue[1]=20 Dequeue: front=0 (removes queue[0]=10) Enqueue 30: rear=2, queue[2]=30
  2. Step 2: Identify current queue elements

    Queue elements are from front+1=1 to rear=2, so queue[1]=20 and queue[2]=30. Printed elements: [20, 30]
  3. Final Answer:

    [20, 30] -> Option A
  4. Quick Check:

    After dequeue, front=0; elements = queue[1..2] = [20,30] [OK]
Hint: Queue elements lie between front+1 and rear indices [OK]
Common Mistakes:
  • Printing elements from 0 to rear ignoring front
  • Not updating front after dequeue
  • Confusing front and rear indices
4. Given this Go code snippet for dequeue operation in a queue implemented with array:
func dequeue() int {
    if front == rear {
        fmt.Println("Queue is empty")
        return -1
    }
    front++
    return queue[front]
}
What is the main bug in this code?
medium
A. It does not check if queue is full before dequeue
B. It should decrement rear instead of front
C. It increments front before returning element, causing off-by-one error
D. It returns queue[front] after increment, which is correct

Solution

  1. Step 1: Understand dequeue logic

    Dequeue removes element at front+1 index. Incrementing front before returning queue[front] is correct to get the next element.
  2. Step 2: Analyze options for bug

    It does not check if queue is full before dequeue is unrelated to dequeue. It increments front before returning element, causing off-by-one error says increment causes off-by-one error but this is standard for queue. It should decrement rear instead of front is wrong because rear is for enqueue. It returns queue[front] after increment, which is correct correctly states the code is correct.
  3. Final Answer:

    It returns queue[front] after increment, which is correct -> Option D
  4. Quick Check:

    Increment front then return element = correct dequeue [OK]
Hint: Dequeue increments front then returns element [OK]
Common Mistakes:
  • Thinking front should not be incremented before return
  • Confusing enqueue and dequeue pointers
  • Checking full condition in dequeue
5. You have a fixed-size queue implemented using an array of size 5 with front and rear indices initialized to -1. After performing these operations:
Enqueue 5
Enqueue 10
Enqueue 15
Dequeue
Dequeue
Enqueue 20
Enqueue 25
Enqueue 30
Enqueue 35
What will happen when you try to enqueue 35, and what is the correct way to handle this situation?
hard
A. Queue underflow occurs; reset front and rear to -1
B. Queue overflow occurs; use circular queue logic to reuse space
C. Queue accepts 35 without issue; no special handling needed
D. Queue overflow occurs; increase array size dynamically

Solution

  1. Step 1: Trace queue operations and indices

    Initial front = -1, rear = -1 Enqueue 5: rear=0 Enqueue 10: rear=1 Enqueue 15: rear=2 Dequeue: front=0 (removes 5) Dequeue: front=1 (removes 10) Enqueue 20: rear=3 Enqueue 25: rear=4 Enqueue 30: rear=5 (array size 5, index 5 invalid - overflow) Trying to enqueue 35 causes overflow
  2. Step 2: Explain correct handling

    Since front has moved forward, space at start is free but rear reached end. Using circular queue logic allows rear to wrap to start and reuse space. Without circular logic, overflow occurs prematurely.
  3. Final Answer:

    Queue overflow occurs; use circular queue logic to reuse space -> Option B
  4. Quick Check:

    Overflow at end; circular queue fixes space reuse [OK]
Hint: Use circular queue to avoid overflow when space is free [OK]
Common Mistakes:
  • Ignoring free space at start after dequeues
  • Assuming array can grow dynamically
  • Resetting front and rear incorrectly
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