Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a graph node with a label.
DSA Go
type Node struct {
label string
neighbors []*Node
}
func NewNode(label string) *Node {
return &Node{label: [1], neighbors: []*Node{}}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using "label" (a string literal) instead of the variable label.
Assigning neighbors instead of label.
✗ Incorrect
The label field should be assigned the variable 'label' passed to the function, not a string literal or other value.
2fill in blank
mediumComplete the code to add an edge from one node to another in an undirected graph.
DSA Go
func AddEdge(n1, n2 *Node) {
n1.neighbors = append(n1.neighbors, [1])
n2.neighbors = append(n2.neighbors, n1)
}Attempts:
3 left
💡 Hint
Common Mistakes
Appending n1 to its own neighbors.
Appending neighbors instead of a node.
✗ Incorrect
To add an edge from n1 to n2, append n2 to n1's neighbors slice.
3fill in blank
hardFix the error in the function that checks if a graph contains a cycle using DFS.
DSA Go
func hasCycle(node *Node, visited map[*Node]bool, parent *Node) bool {
visited[node] = true
for _, neighbor := range node.neighbors {
if !visited[[1]] {
if hasCycle(neighbor, visited, node) {
return true
}
} else if neighbor != parent {
return true
}
}
return false
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking visited[node] instead of visited[neighbor].
Using parent or visited as index incorrectly.
✗ Incorrect
We check if the neighbor node has been visited, so the condition should be !visited[neighbor].
4fill in blank
hardFill both blanks to create a map of node labels to their neighbors' labels.
DSA Go
func LabelNeighbors(nodes []*Node) map[string][]string {
result := make(map[string][]string)
for _, node := range nodes {
neighbors := []string{}
for _, neighbor := range node.[1] {
neighbors = append(neighbors, neighbor.[2])
}
result[node.label] = neighbors
}
return result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using node.label instead of node.neighbors in the first blank.
Using neighbor.neighbors instead of neighbor.label in the second blank.
✗ Incorrect
To access neighbors of a node, use node.neighbors, and to get the label of a neighbor, use neighbor.label.
5fill in blank
hardFill all three blanks to implement a BFS traversal that returns node labels in visit order.
DSA Go
func BFS(start *Node) []string {
visited := make(map[*Node]bool)
queue := []*Node{start}
visited[start] = true
order := []string{}
for len(queue) > 0 {
current := queue[[1]]
queue = queue[[2]:]
order = append(order, current.[3])
for _, neighbor := range current.neighbors {
if !visited[neighbor] {
visited[neighbor] = true
queue = append(queue, neighbor)
}
}
}
return order
}Attempts:
3 left
💡 Hint
Common Mistakes
Using len(queue) or 1 as index instead of 0.
Appending current instead of current.label.
✗ Incorrect
In BFS, we dequeue from the front (index 0), so current = queue[0], then remove the first element with queue = queue[1:], and append current.label to order.
