Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the adjacency list for the graph.
DSA C#
List<int>[] graph = new List<int>[[1]];Attempts:
3 left
💡 Hint
Common Mistakes
Using number of edges instead of vertices for array size.
Using variable names instead of actual size.
✗ Incorrect
The adjacency list array size should be the number of vertices, here 5.
2fill in blank
mediumComplete the code to add an edge from node u to node v in the adjacency list.
DSA C#
graph[[1]].Add(v);Attempts:
3 left
💡 Hint
Common Mistakes
Adding u to v's list instead of v to u's list.
Using wrong variable names.
✗ Incorrect
We add v to the adjacency list of u to represent an edge u -> v.
3fill in blank
hardFix the error in the DFS function call to visit neighbors.
DSA C#
foreach (int neighbor in graph[node]) { if (!visited[neighbor]) { DFS([1], visited, stack, graph); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Calling DFS on the same node repeatedly.
Passing wrong parameters to DFS.
✗ Incorrect
We must call DFS on the neighbor node, not the current node.
4fill in blank
hardFill both blanks to correctly push the node after visiting neighbors and mark it visited.
DSA C#
visited[[1]] = true; stack.[2](node);
Attempts:
3 left
💡 Hint
Common Mistakes
Marking neighbor visited instead of node.
Using Pop instead of Push on stack.
✗ Incorrect
Mark the current node visited and push it onto the stack after DFS.
5fill in blank
hardFill all three blanks to create the topological sort function using DFS.
DSA C#
public static List<int> TopologicalSort(List<int>[] graph, int [1]) { bool[] visited = new bool[[2]]; Stack<int> stack = new Stack<int>(); for (int i = 0; i < [3]; i++) { if (!visited[i]) DFS(i, visited, stack, graph); } List<int> result = new List<int>(); while (stack.Count > 0) result.Add(stack.Pop()); return result; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for sizes.
Looping beyond graph length.
✗ Incorrect
Use n as parameter and loop limit, graph.Length for visited array size.
