📖 Scenario: You are managing a line of customers waiting to buy tickets at a counter. You want to check who is the first customer in line without removing them.
🎯 Goal: Build a simple queue using a slice and write code to peek at the front element (the first customer) without removing it.
📋 What You'll Learn
Create a queue as a slice of strings with exact customer names
Create a variable to hold the front element
Write code to peek at the front element of the queue
Print the front element
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines, like customers waiting for service or tasks waiting to be processed.
💼 Career
Understanding queues and how to peek at elements is important for software developers working with data processing, task scheduling, and user interface event handling.
Progress0 / 4 steps
1
Create the queue with customers
Create a slice of strings called queue with these exact customers in order: "Alice", "Bob", "Charlie", "Diana".
DSA Go
Hint
Use a slice literal with the exact names in order.
2
Create a variable to hold the front element
Create a variable called front of type string to hold the first element of the queue slice.
DSA Go
Hint
Initialize front as an empty string first.
3
Peek the front element of the queue
Assign the first element of the queue slice to the variable front using index 0.
DSA Go
Hint
Use queue[0] to get the first element.
4
Print the front element
Use fmt.Println(front) to print the value of the front variable.
DSA Go
Hint
Use fmt.Println(front) to display the first customer.
Practice
(1/5)
1. What does the Peek operation do in a queue?
easy
A. Returns the front element without removing it
B. Removes the front element from the queue
C. Adds an element to the back of the queue
D. Checks if the queue is empty
Solution
Step 1: Understand the queue operations
A queue supports adding elements at the back and removing from the front.
Step 2: Define Peek operation
Peek returns the front element without removing it, unlike dequeue which removes it.
Final Answer:
Returns the front element without removing it -> Option A
Quick Check:
Peek = front element without removal [OK]
Hint: Peek shows front without removal, unlike dequeue [OK]
Common Mistakes:
Confusing peek with dequeue
Thinking peek adds elements
Assuming peek checks emptiness
2. Which of the following is the correct Go syntax to peek the front element of a queue implemented as a slice named queue?
easy
A. front := queue[len(queue)-1]
B. front := queue[len(queue)]
C. front := queue[0]
D. front := queue.pop()
Solution
Step 1: Understand Go slice indexing
Go slices are zero-indexed, so the first element is at index 0.
Step 2: Identify front element access
The front element of a queue is the first element, so queue[0] gives it.
Final Answer:
front := queue[0] -> Option C
Quick Check:
First element index = 0 [OK]
Hint: Front element is always at index 0 in slice queue [OK]
Common Mistakes:
Using len(queue) which is out of range
Using last element instead of first
Using pop() which is not a Go slice method
3. What will be the output of the following Go code snippet?
queue := []int{10, 20, 30}
front := queue[0]
fmt.Println(front)
medium
A. runtime error
B. 10
C. 30
D. 20
Solution
Step 1: Initialize queue slice
The queue slice contains elements [10, 20, 30].
Step 2: Access front element
front := queue[0] assigns 10 to front.
Step 3: Print front
fmt.Println(front) prints 10.
Final Answer:
10 -> Option B
Quick Check:
queue[0] = 10 [OK]
Hint: queue[0] gives first element value [OK]
Common Mistakes:
Printing queue[1] instead of queue[0]
Expecting last element output
Thinking it causes runtime error
4. Identify the error in this Go code that tries to peek the front element of a queue:
var queue []int
front := queue[0]
fmt.Println(front)
medium
A. No error, prints 0
B. Prints nil
C. Compile-time syntax error
D. Runtime panic: index out of range
Solution
Step 1: Check queue initialization
queue is declared but not initialized, so length is 0.
Step 2: Accessing queue[0]
Accessing queue[0] causes runtime panic because slice is empty.
5. Given a queue implemented as a slice queue := []int{5, 10, 15}, write the correct Go code snippet to safely peek the front element and handle the empty queue case by printing "Empty" if no elements exist.
hard
A. if len(queue) > 0 {
fmt.Println(queue[0])
} else {
fmt.Println("Empty")
}
B. fmt.Println(queue[0])
C. if queue != nil {
fmt.Println(queue[0])
} else {
fmt.Println("Empty")
}
D. if len(queue) == 0 {
fmt.Println(queue[0])
} else {
fmt.Println("Empty")
}
Solution
Step 1: Check queue length before access
Use len(queue) > 0 to ensure queue has elements.
Step 2: Print front or "Empty"
If queue has elements, print queue[0]; else print "Empty".
Final Answer:
if len(queue) > 0 {
fmt.Println(queue[0])
} else {
fmt.Println("Empty")
} -> Option A
Quick Check:
Check length before access to avoid panic [OK]
Hint: Always check length before peeking front [OK]
Common Mistakes:
Not checking length before access
Checking nil instead of length
Reversing if-else logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.