D. Index out of range error because slice index 3 does not exist
Solution
Step 1: Analyze slice indexing
Slice 'queue' has length 3 with indices 0,1,2; index 3 is out of range.
Step 2: Understand how to add elements
To add elements, use append(queue, 4) instead of direct index assignment.
Final Answer:
Index out of range error because slice index 3 does not exist -> Option D
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
Step 1: Understand enqueue multiple elements
To add multiple elements preserving order, append all new elements at the end of queue.
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.
Final Answer:
return append(queue, elements...) -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.