Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add an element at the front of the deque.
DSA Go
func (d *Deque) AddFront(value int) {
d.items = append([]int{value}, [1]...)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'items' instead of 'd.items' causes undefined variable error.
Appending value instead of d.items leads to wrong order.
✗ Incorrect
We append the existing items slice after the new value to add at the front.
2fill in blank
mediumComplete the code to remove and return the last element from the deque.
DSA Go
func (d *Deque) RemoveBack() int {
if len(d.items) == 0 {
return -1
}
last := d.items[len(d.items)[1]]
d.items = d.items[:len(d.items)-1]
return last
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '-2' accesses the second last element.
Using '-0' results in index len(d.items) which is out of range.
✗ Incorrect
Index len(d.items)-1 gives the last element.
3fill in blank
hardFix the error in the code to check if the deque is empty.
DSA Go
func (d *Deque) IsEmpty() bool {
return len([1]) == 0
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'items' alone causes undefined variable error.
Using 'd' alone is not a slice.
✗ Incorrect
The items slice is stored in d.items, so we check its length.
4fill in blank
hardFill both blanks to add an element at the back of the deque correctly.
DSA Go
func (d *Deque) AddBack(value int) {
d.items = append([1], [2])
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order causes type errors.
Using 'items' instead of 'd.items' causes undefined variable error.
✗ Incorrect
We append the new value to the existing d.items slice.
5fill in blank
hardFill all three blanks to remove and return the front element from the deque.
DSA Go
func (d *Deque) RemoveFront() int {
if len(d.items) == 0 {
return [1]
}
front := d.items[[2]]
d.items = d.items[[3]:]
return front
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of -1 for empty deque.
Using wrong slice indices causing runtime errors.
✗ Incorrect
Return -1 if empty, front element is at index 0, then slice from index 1 onward.
