Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the DFS function parameter for the graph adjacency list.
DSA C
void dfs(int node, int visited[], vector<vector<int>> [1], int n) { Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined or inconsistent with the rest of the code.
✗ Incorrect
The adjacency list is commonly named 'adj' to represent graph connections.
2fill in blank
mediumComplete the code to mark the current node as visited inside DFS.
DSA C
visited[node] = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Marking visited[node] as 0 which means unvisited.
✗ Incorrect
Marking visited[node] as 1 means the node is visited.
3fill in blank
hardFix the error in the DFS recursive call to visit adjacent nodes.
DSA C
for(int i = 0; i < adj[node].size(); i++) { int adjNode = adj[node][i]; if(!visited[[1]]) { dfs(adjNode, visited, adj, n); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking visited[node] instead of visited[adjNode].
Using the loop index 'i' as the node index.
✗ Incorrect
We check if the adjacent node 'adjNode' is visited before calling DFS on it.
4fill in blank
hardFill both blanks to correctly initialize and call DFS for all nodes in the graph.
DSA C
int visited[n]; for(int i = 0; i < n; i++) { visited[i] = [1]; } for(int i = 0; i < n; i++) { if(visited[i] == [2]) { dfs(i, visited, adj, n); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing visited array with 1 instead of 0.
Checking for visited[i] == 1 instead of 0.
✗ Incorrect
Initialize all nodes as unvisited (0) and call DFS on nodes that are unvisited (0).
5fill in blank
hardFill all three blanks to complete the DFS function that prints nodes in DFS order.
DSA C
void dfs(int node, int visited[], vector<vector<int>> [1], int n) { visited[node] = [2]; printf("%d ", [3]); for(int i = 0; i < adj[node].size(); i++) { int adjNode = adj[node][i]; if(!visited[adjNode]) { dfs(adjNode, visited, adj, n); } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Printing visited[node] instead of node.
Using wrong variable name for adjacency list.
✗ Incorrect
The adjacency list is 'adj', visited[node] is set to 1, and we print the current node.