Bird
Raised Fist0
DSA Goprogramming~20 mins

Queue Concept and FIFO Principle 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 on a Queue
What is the printed state of the queue after performing these operations in order: enqueue 10, enqueue 20, dequeue, enqueue 30?
DSA Go
package main

import "fmt"

type Queue struct {
    items []int
}

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

func (q *Queue) Dequeue() int {
    if len(q.items) == 0 {
        return -1
    }
    val := q.items[0]
    q.items = q.items[1:]
    return val
}

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

func main() {
    q := Queue{}
    q.Enqueue(10)
    q.Enqueue(20)
    q.Dequeue()
    q.Enqueue(30)
    q.Print()
}
A10 -> 30 -> null
B20 -> 30 -> null
C30 -> 20 -> null
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember that dequeue removes the first element added (FIFO).
🧠 Conceptual
intermediate
1:30remaining
Understanding FIFO Principle in Queues
Which statement best describes the FIFO principle in queues?
AElements are removed randomly regardless of order.
BThe last element added is the first one to be removed.
CElements are removed based on their value, smallest first.
DElements are removed in the same order they were added.
Attempts:
2 left
💡 Hint
Think about a line of people waiting for service.
🔧 Debug
advanced
2:00remaining
Identify the Error in Queue Dequeue Implementation
What error will occur when running this dequeue method if the queue is empty?
DSA Go
func (q *Queue) Dequeue() int {
    val := q.items[0]
    q.items = q.items[1:]
    return val
}
ACompilation error due to missing return
BReturns -1 indicating empty queue
CIndex out of range runtime error
DNo error, returns zero value
Attempts:
2 left
💡 Hint
What happens if you access the first element of an empty slice?
❓ Predict Output
advanced
2:00remaining
Final Queue State After Multiple Enqueue and Dequeue
What is the printed queue after these operations: enqueue 5, enqueue 10, enqueue 15, dequeue, dequeue, enqueue 20?
DSA Go
package main

import "fmt"

type Queue struct {
    items []int
}

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

func (q *Queue) Dequeue() int {
    if len(q.items) == 0 {
        return -1
    }
    val := q.items[0]
    q.items = q.items[1:]
    return val
}

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

func main() {
    q := Queue{}
    q.Enqueue(5)
    q.Enqueue(10)
    q.Enqueue(15)
    q.Dequeue()
    q.Dequeue()
    q.Enqueue(20)
    q.Print()
}
A15 -> 20 -> null
B10 -> 15 -> 20 -> null
C5 -> 20 -> null
D20 -> null
Attempts:
2 left
💡 Hint
Dequeue removes from the front, enqueue adds to the back.
🧠 Conceptual
expert
2:30remaining
Queue Behavior in Concurrent Environments
In a concurrent system where multiple threads enqueue and dequeue from a shared queue without synchronization, what is the most likely problem?
ARace conditions may cause corrupted queue state or lost elements.
BDequeue operations will fail silently without removing elements.
CQueue operations will block automatically until safe.
DQueue will always maintain perfect FIFO order.
Attempts:
2 left
💡 Hint
Think about what happens if two threads modify the queue at the same time.

Practice

(1/5)
1. What does the FIFO principle mean in the context of a queue?
easy
A. First In, First Out - the last element added is the first removed
B. First In, First Out - the first element added is the first removed
C. Last In, First Out - the first element added is the last removed
D. Last In, First Out - the last element added is the last removed

Solution

  1. Step 1: Understand FIFO meaning

    FIFO means the first element that enters the queue is the first to leave.
  2. Step 2: Match FIFO to queue behavior

    Queues follow FIFO, so the first added element is removed first.
  3. Final Answer:

    First In, First Out - the first element added is the first removed -> Option B
  4. Quick Check:

    FIFO = First In, First Out [OK]
Hint: Remember queue = line; first come, first served [OK]
Common Mistakes:
  • Confusing FIFO with LIFO
  • Thinking last added is removed first
  • Mixing stack behavior with queue
2. Which of the following is the correct way to declare a queue using a slice in Go?
easy
A. queue := []int{}
B. var queue []int
C. queue := make(map[int]int)
D. var queue int

Solution

  1. Step 1: Identify slice declaration for queue

    In Go, a queue can be represented as a slice, initialized as empty with []int{}.
  2. Step 2: Check options for correct slice syntax

    queue := []int{} uses slice literal syntax to create an empty int slice, suitable for queue.
  3. Final Answer:

    queue := []int{} -> Option A
  4. Quick Check:

    Slice queue declaration = queue := []int{} [OK]
Hint: Use slice literal []int{} for empty queue [OK]
Common Mistakes:
  • Using map instead of slice
  • Declaring queue as single int variable
  • Using var without initialization for slice
3. What will be the output of the following Go code snippet?
package main
import "fmt"
func main() {
  queue := []int{10, 20, 30}
  // Dequeue operation
  first := queue[0]
  queue = queue[1:]
  fmt.Println(first)
  fmt.Println(queue)
}
medium
A. 10 [10 20 30]
B. 20 [10 30]
C. 10 [20 30]
D. 30 [10 20]

Solution

  1. Step 1: Identify dequeue operation

    The first element 10 is stored in 'first', then queue is sliced from index 1 onward.
  2. Step 2: Determine output values

    Printing 'first' outputs 10; printing 'queue' outputs remaining slice [20 30].
  3. Final Answer:

    10 [20 30] -> Option C
  4. Quick Check:

    Dequeue removes first element = 10, queue left = [20 30] [OK]
Hint: Dequeue removes first element, slice queue from index 1 [OK]
Common Mistakes:
  • Printing old queue without slicing
  • Confusing index positions
  • Assuming queue unchanged after dequeue
4. Identify the error in the following Go code that tries to enqueue an element to a queue represented by a slice:
package main
func main() {
  queue := []int{1, 2, 3}
  queue[3] = 4
}
medium
A. Compile error because queue is not declared
B. Syntax error due to missing import statement
C. No error; element 4 is added correctly
D. Index out of range error because slice index 3 does not exist

Solution

  1. Step 1: Analyze slice indexing

    Slice 'queue' has length 3 with indices 0,1,2; index 3 is out of range.
  2. Step 2: Understand how to add elements

    To add elements, use append(queue, 4) instead of direct index assignment.
  3. Final Answer:

    Index out of range error because slice index 3 does not exist -> Option D
  4. Quick Check:

    Accessing queue[3] invalid for length 3 slice [OK]
Hint: Use append to add elements, not direct index assignment [OK]
Common Mistakes:
  • Trying to assign beyond slice length
  • Forgetting to use append for enqueue
  • Assuming slice auto-expands on index assignment
5. Given a queue implemented as a slice in Go, how would you write a function to enqueue multiple elements at once while preserving FIFO order? Choose the correct code snippet.
func enqueueMultiple(queue []int, elements []int) []int {
  // Fill in
}
hard
A. return append(queue, elements...)
B. return append(elements, queue...)
C. for _, e := range elements { queue = append([]int{e}, queue...) } return queue
D. queue = elements; return queue

Solution

  1. Step 1: Understand enqueue multiple elements

    To add multiple elements preserving order, append all new elements at the end of queue.
  2. Step 2: Check code options for correct append usage

    return append(queue, elements...) uses append(queue, elements...) which adds all elements at the end in order.
  3. Final Answer:

    return append(queue, elements...) -> Option A
  4. Quick Check:

    Appending elements at end preserves FIFO enqueue order [OK]
Hint: Use append(queue, elements...) to add multiple items [OK]
Common Mistakes:
  • Appending new elements before queue (wrong order)
  • Replacing queue instead of appending
  • Appending elements one by one at front
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