Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the visited slice for all nodes.
DSA Go
visited := make([]bool, [1])Attempts:
3 left
💡 Hint
Common Mistakes
Using cap(graph) instead of len(graph)
Initializing visited with length 0 or 1
✗ Incorrect
We need to create a visited slice with length equal to the number of nodes in the graph, which is len(graph).
2fill in blank
mediumComplete the code to mark the current node as being in the recursion stack.
DSA Go
recStack[[1]] = trueAttempts:
3 left
💡 Hint
Common Mistakes
Using graph or visited instead of node as index
Using an undefined variable like index
✗ Incorrect
We mark the current node index as true in recStack to indicate it is in the recursion stack.
3fill in blank
hardFix the error in the DFS function to detect a cycle when a node is already in the recursion stack.
DSA Go
if recStack[neighbor] [1] true { return true }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '=='
Using comparison operators like '<' or '>' on booleans
✗ Incorrect
We check if recStack[neighbor] == true to detect a cycle in the recursion stack.
4fill in blank
hardFill both blanks to complete the DFS function that marks nodes visited and removes them from recursion stack.
DSA Go
visited[[1]] = true recStack[[2]] = false
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for visited and recStack
Using neighbor or index instead of node
✗ Incorrect
We mark the current node as visited and remove it from the recursion stack using the same node index.
5fill in blank
hardFill all three blanks to complete the cycle detection function using DFS.
DSA Go
for [1] := 0; [2] < len(graph); [3]++ { if !visited[i] { if dfs(i, graph, visited, recStack) { return true } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in loop parts
Using 'node' instead of 'i' in the loop
✗ Incorrect
The loop variable is 'i' used for initialization, condition, and increment to iterate over graph nodes.
