Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a graph has cycles.
DSA Go
func hasCycle(graph map[int][]int) bool {
visited := make(map[int]bool)
var dfs func(node int, parent int) bool
dfs = func(node int, parent int) bool {
visited[node] = true
for _, neighbor := range graph[node] {
if !visited[neighbor] {
if dfs(neighbor, node) [1] {
return true
}
} else if neighbor != parent {
return true
}
}
return false
}
for node := range graph {
if !visited[node] {
if dfs(node, -1) {
return true
}
}
}
return false
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '== false' instead of '== true' causes missing cycle detection.
Not checking the recursive call result leads to incorrect results.
✗ Incorrect
The DFS function returns true if a cycle is found. So we check if dfs(...) == true to return true.
2fill in blank
mediumComplete the code to add an edge in an undirected graph.
DSA Go
func addEdge(graph map[int][]int, u int, v int) {
graph[u] = append(graph[u], [1])
graph[v] = append(graph[v], u)
}Attempts:
3 left
💡 Hint
Common Mistakes
Appending u to graph[u] instead of v.
Appending adjacency lists instead of node values.
✗ Incorrect
To add an edge from u to v, append v to graph[u].
3fill in blank
hardFix the error in the code to check if a tree is connected.
DSA Go
func isConnected(graph map[int][]int, n int) bool {
visited := make(map[int]bool)
var dfs func(node int)
dfs = func(node int) {
visited[node] = true
for _, neighbor := range graph[node] {
if !visited[neighbor] {
dfs([1])
}
}
}
dfs(0)
return len(visited) == n
}Attempts:
3 left
💡 Hint
Common Mistakes
Recursing on the current node instead of neighbor.
Using graph[node] which is a slice, not a node.
✗ Incorrect
The DFS should recurse on the neighbor node, not the current node.
4fill in blank
hardFill both blanks to create a map of node degrees in a graph.
DSA Go
func nodeDegrees(graph map[int][]int) map[int]int {
degrees := make(map[int]int)
for node, neighbors := range graph {
degrees[node] = [1](neighbors)
for _, neighbor := range neighbors {
if _, ok := degrees[neighbor]; !ok {
degrees[neighbor] = [2]
}
}
}
return degrees
}Attempts:
3 left
💡 Hint
Common Mistakes
Using cap() instead of len() for length.
Initializing unseen neighbors with 1 instead of 0.
✗ Incorrect
The degree of a node is the length of its neighbors slice. Initialize unseen neighbors with 0.
5fill in blank
hardFill all three blanks to create a function that returns true if a graph is a tree.
DSA Go
func isTree(graph map[int][]int, n int) bool {
if len(graph) != n {
return false
}
visited := make(map[int]bool)
var dfs func(node int, parent int) bool
dfs = func(node int, parent int) bool {
visited[node] = true
for _, neighbor := range graph[node] {
if !visited[neighbor] {
if dfs(neighbor, node) [1] {
return true
}
} else if neighbor != [2] {
return true
}
}
return false
}
if dfs(0, -1) [3] {
return false
}
return len(visited) == n
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' false instead of '==' true for cycle detection.
Comparing neighbor to node instead of parent.
Returning true when dfs returns false.
✗ Incorrect
Detect cycles by checking if dfs returns true. Check if neighbor is not the parent to detect back edges. If dfs returns false, no cycle found.
