Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with given value.
DSA Go
func newNode(value int) *Node {
return &Node{data: [1], next: nil, prev: nil}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined variable like 'val' or 'node' instead of 'value'.
Confusing the field name 'data' with the parameter name.
✗ Incorrect
The new node's data field should be set to the input parameter 'value'.
2fill in blank
mediumComplete the code to add a node at the front of the dequeue.
DSA Go
func (dq *Dequeue) AddFront(value int) {
node := newNode(value)
if dq.front == nil {
dq.front = node
dq.rear = node
} else {
node.next = dq.front
dq.front.[1] = node
dq.front = node
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting the 'next' pointer instead of 'prev' on the old front node.
Assigning to 'front' or 'rear' instead of the pointer field.
✗ Incorrect
When adding at front, the old front's previous pointer should point to the new node.
3fill in blank
hardFix the error in removing a node from the rear of the dequeue.
DSA Go
func (dq *Dequeue) RemoveRear() (int, bool) {
if dq.rear == nil {
return 0, false
}
value := dq.rear.data
dq.rear = dq.rear.[1]
if dq.rear != nil {
dq.rear.next = nil
} else {
dq.front = nil
}
return value, true
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' pointer which moves forward instead of backward.
Assigning to 'front' or 'rear' incorrectly.
✗ Incorrect
To remove from rear, move rear pointer to previous node using 'prev'.
4fill in blank
hardFill both blanks to check if the dequeue is empty and return its size.
DSA Go
func (dq *Dequeue) IsEmpty() bool {
return dq.[1] == nil
}
func (dq *Dequeue) Size() int {
count := 0
current := dq.[2]
for current != nil {
count++
current = current.next
}
return count
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking rear instead of front for emptiness.
Starting size count from rear instead of front.
✗ Incorrect
Empty means front is nil; size counts nodes starting from front.
5fill in blank
hardFill all three blanks to implement adding a node at the rear of the dequeue.
DSA Go
func (dq *Dequeue) AddRear(value int) {
node := newNode(value)
if dq.rear == nil {
dq.front = node
dq.rear = node
} else {
dq.rear.[1] = node
node.[2] = dq.rear
dq.rear = node
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up next and prev pointers.
Not updating the rear pointer after adding.
✗ Incorrect
Link old rear's next to new node, new node's prev to old rear, then update rear pointer.
