Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with given data.
DSA Go
func newNode(data int) *Node {
return &Node{
data: [1],
next: nil,
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of 'data' for the node's data field.
Assigning nil to data field.
✗ Incorrect
The new node's data field should be set to the input parameter 'data'.
2fill in blank
mediumComplete the code to check if the queue is empty.
DSA Go
func (q *Queue) isEmpty() bool {
return [1] == nil
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking tail instead of front.
Checking the queue struct itself instead of its pointers.
✗ Incorrect
The queue is empty if the front pointer is nil.
3fill in blank
hardFix the error in the enqueue method to correctly add a new node at the end.
DSA Go
func (q *Queue) enqueue(data int) {
node := newNode(data)
if q.tail == nil {
q.front = node
q.tail = node
} else {
q.tail.[1] = node
q.tail = node
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning to tail instead of tail.next.
Assigning to front instead of tail.next.
✗ Incorrect
The current tail's next pointer should point to the new node.
4fill in blank
hardFill both blanks to correctly dequeue an element and update the queue pointers.
DSA Go
func (q *Queue) dequeue() (int, bool) {
if q.isEmpty() {
return 0, false
}
data := q.front.data
q.front = q.front.[1]
if q.front == [2] {
q.tail = nil
}
return data, true
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting tail to front instead of nil.
Not updating front pointer correctly.
✗ Incorrect
After removing the front node, move front to front.next. If front becomes nil, set tail to nil.
5fill in blank
hardFill all three blanks to implement a method that returns the queue elements as a slice.
DSA Go
func (q *Queue) toSlice() []int {
var result []int
current := q.front
for current != [1] {
result = append(result, current.[2])
current = current.[3]
}
return result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using front instead of nil as loop end condition.
Appending next instead of data.
Moving current to data instead of next.
✗ Incorrect
Traverse nodes until current is nil, appending current.data and moving to current.next each time.
