Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the adjacency list for a directed graph with n vertices.
DSA C++
vector<vector<int>> graph([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using edges instead of n for adjacency list size.
Initializing with 0 which creates an empty list with no vertices.
✗ Incorrect
The adjacency list is initialized with n empty vectors, one for each vertex.
2fill in blank
mediumComplete the code to mark the current node as visited in the DFS function.
DSA C++
visited[node] = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Marking visited[node] as false which means unvisited.
Using 0 or 1 instead of true or false in boolean context.
✗ Incorrect
Marking visited[node] as true indicates the node is currently being explored.
3fill in blank
hardFix the error in the condition to detect a cycle when a node is in the recursion stack.
DSA C++
if (recStack[[1]]) return true;
Attempts:
3 left
💡 Hint
Common Mistakes
Checking parent instead of node in recursion stack.
Using visited or graph which are not boolean arrays for recursion stack.
✗ Incorrect
We check if the current node is already in the recursion stack to detect a cycle.
4fill in blank
hardFill both blanks to correctly remove the node from recursion stack and mark it as not visited after DFS call.
DSA C++
recStack[[1]] = false; visited[[2]] = false;
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for recStack and visited updates.
Marking visited[node] as false which is incorrect for cycle detection.
✗ Incorrect
After exploring all neighbors, remove the current node from recursion stack and mark it as not in recursion stack.
5fill in blank
hardFill all three blanks to complete the DFS function header and recursive call for cycle detection.
DSA C++
bool dfs(int [1], vector<vector<int>>& graph, vector<bool>& visited, vector<bool>& [2]) { visited[node] = true; recStack[node] = true; for (int [3] : graph[node]) { if (!visited[neighbor] && dfs(neighbor, graph, visited, recStack)) return true; else if (recStack[neighbor]) return true; } recStack[node] = false; return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names causing compilation errors.
Mixing up visited and recStack variable names.
✗ Incorrect
The DFS function uses 'node' as current node, 'recStack' for recursion stack, and 'neighbor' for adjacent nodes.
