Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a doubly linked list node with data and pointers.
DSA Go
type Node struct {
data int
prev *Node
[1] *Node
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'previous' instead of 'next' for the forward pointer.
Using generic names like 'link' or 'pointer' instead of 'next'.
✗ Incorrect
The 'next' field points to the next node in the doubly linked list.
2fill in blank
mediumComplete the code to initialize a new node with given data.
DSA Go
func NewNode(data int) *Node {
return &Node{
data: [1],
prev: nil,
next: nil,
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of the input data parameter.
Using '&data' or '*data' which are incorrect here.
✗ Incorrect
The 'data' field is set to the input parameter 'data' to store the node's value.
3fill in blank
hardFix the error in the method to set the next node pointer.
DSA Go
func (n *Node) SetNext([1] *Node) {
n.next = node
}Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from the variable assigned.
Using 'nextNode' in assignment but not as parameter.
✗ Incorrect
The parameter name should be 'node' to match the assignment 'n.next = node'.
4fill in blank
hardFill both blanks to correctly link two nodes in a doubly linked list.
DSA Go
func LinkNodes(first, second *Node) {
first.[1] = second
second.[2] = first
}Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'next' and 'prev' fields.
Using incorrect field names like 'link' or 'pointer'.
✗ Incorrect
The first node's 'next' points to the second, and the second node's 'prev' points back to the first.
5fill in blank
hardFill all three blanks to create a new node and link it after an existing node.
DSA Go
func InsertAfter(existing *Node, data int) *Node {
newNode := &Node{data: [1], prev: existing, next: existing.[2]
if existing.[3] != nil {
existing.next.prev = newNode
}
existing.next = newNode
return newNode
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' when accessing existing node's next pointer.
Not updating the previous pointer of the node after the new node.
✗ Incorrect
The new node's data is set to 'data', its next pointer to existing.next, and the existing node's next pointer is used to update the previous pointer of the next node.
