Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to peek the front element of the queue.
DSA Go
func Peek(queue []int) int {
if len(queue) == 0 {
return -1
}
return queue[[1]]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the last element instead of the first.
Using slicing instead of indexing.
✗ Incorrect
The front element of a queue is the first element, which is at index 0 in the slice.
2fill in blank
mediumComplete the code to check if the queue is empty before peeking.
DSA Go
func Peek(queue []int) (int, bool) {
if len(queue) [1] 0 {
return 0, false
}
return queue[0], true
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '<=' which do not correctly check for empty queue.
Using '!=' which checks for non-empty queue.
✗ Incorrect
We check if the length of the queue is equal to 0 to determine if it is empty.
3fill in blank
hardFix the error in the code to correctly peek the front element.
DSA Go
func Peek(queue []int) int {
if len(queue) == 0 {
return -1
}
return queue[[1]]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 which is the second element.
Using slicing which returns a slice, not an element.
✗ Incorrect
The front element is at index 0, so we must use queue[0] to peek it.
4fill in blank
hardFill both blanks to create a function that peeks the front element safely.
DSA Go
func Peek(queue []int) (int, bool) {
if len(queue) [1] 0 {
return 0, false
}
return queue[2], true
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '==' to check empty queue.
Accessing wrong index for front element.
✗ Incorrect
Check if length equals zero to detect empty queue, then return the first element at index 0.
5fill in blank
hardFill all three blanks to implement a safe peek function returning front element and success flag.
DSA Go
func Peek(queue []int) (int, bool) {
if len(queue) [1] 0 {
return 0, false
}
front := queue[2]
return front, [3]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operator.
Accessing wrong index.
Returning false instead of true on success.
✗ Incorrect
Check if length equals zero, access front element at index 0, and return true for success.
