Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the adjacency list for the graph.
DSA C
int adj[[1]][100]; // adjacency list for graph int visited[100];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a size larger than the number of nodes wastes memory.
Using a smaller size causes out-of-bounds errors.
✗ Incorrect
The graph size is set to 10 nodes, so the adjacency list should be declared with size 10.
2fill in blank
mediumComplete the DFS function header to accept the current node.
DSA C
void dfs(int [1]) { visited[node] = 1; // DFS logic }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that doesn't match the variable used inside the function.
✗ Incorrect
The parameter name 'node' clearly represents the current node in DFS traversal.
3fill in blank
hardFix the error in the DFS recursive call to visit adjacent nodes.
DSA C
for (int i = 0; i < adj[node][0]; i++) { int adjNode = adj[node][i + 1]; if (!visited[adjNode]) { dfs([1]); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling dfs on the loop index 'i' instead of the adjacent node.
Calling dfs on the current node again causing infinite recursion.
✗ Incorrect
The DFS should be called on the adjacent node, which is 'adjNode'.
4fill in blank
hardFill both blanks to push the node onto the stack after visiting all neighbors.
DSA C
void dfs(int node) {
visited[node] = 1;
for (int i = 0; i < adj[node][0]; i++) {
int adjNode = adj[node][i + 1];
if (!visited[adjNode]) {
dfs(adjNode);
}
}
stack[[1]] = node;
[2]++;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for index and increment causing errors.
Forgetting to increment the stack pointer.
✗ Incorrect
The variable 'top' is used as the stack pointer to store nodes and increment after pushing.
5fill in blank
hardFill all three blanks to print the topological order from the stack.
DSA C
for (int i = [1] - 1; i >= 0; i--) { printf("%d ", stack[[2]]); } printf("\n"); // Reset stack pointer [3] = 0;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names causing compilation errors.
Not resetting the stack pointer after printing.
✗ Incorrect
We start from 'top - 1' down to 0, print stack[i], and reset 'top' to 0 after printing.