Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add an element to the end of the queue.
DSA Go
func (q *Queue) Enqueue(value int) {
q.items = append(q.items, [1])
}Attempts:
3 left
💡 Hint
Common Mistakes
Appending the entire queue instead of the new value.
Using an index instead of the value.
✗ Incorrect
We add the new value to the end of the queue by appending value to q.items.
2fill in blank
mediumComplete the code to remove the first element from the queue.
DSA Go
func (q *Queue) Dequeue() (int, bool) {
if len(q.items) == 0 {
return 0, false
}
item := q.items[0]
q.items = q.items[[1]:]
return item, true
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 which does not remove any element.
Using len(q.items) which empties the queue.
✗ Incorrect
To remove the first element, we slice the queue from index 1 onward.
3fill in blank
hardFix the error in the code to check if the queue is empty.
DSA Go
func (q *Queue) IsEmpty() bool {
return len(q.items) [1] 0
}Attempts:
3 left
💡 Hint
Common Mistakes
Using != which checks if queue is not empty.
Using > or < which are incorrect for emptiness check.
✗ Incorrect
We check if the length of the queue is equal to zero to know if it is empty.
4fill in blank
hardFill both blanks to create a queue and add two elements.
DSA Go
q := &Queue{items: []int{}}
q.Enqueue([1])
q.Enqueue([2])Attempts:
3 left
💡 Hint
Common Mistakes
Adding the same number twice.
Using numbers not in the options.
✗ Incorrect
We add 10 and 20 to the queue in order.
5fill in blank
hardFill all three blanks to create a queue, add elements, and remove one.
DSA Go
q := &Queue{items: []int{}}
q.Enqueue([1])
q.Enqueue([2])
item, ok := q.Dequeue()
fmt.Println(item == [3], ok)Attempts:
3 left
💡 Hint
Common Mistakes
Confusing the order of enqueue and dequeue.
Checking against the wrong value after dequeue.
✗ Incorrect
We add 5 and 15, then remove the first element which is 5.
