Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the in-degree array for all vertices.
DSA C#
int[] inDegree = new int[graph.Length]; for (int i = 0; i < graph.Length; i++) { foreach (int neighbor in graph[i]) { inDegree[[1]]++; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Incrementing inDegree[i] instead of inDegree[neighbor]
Using graph instead of neighbor as index
✗ Incorrect
We increase the in-degree of each neighbor vertex because it has an incoming edge from vertex i.
2fill in blank
mediumComplete the code to enqueue all vertices with zero in-degree.
DSA C#
Queue<int> queue = new Queue<int>(); for (int i = 0; i < inDegree.Length; i++) { if (inDegree[i] == [1]) { queue.Enqueue(i); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for inDegree[i] == 1 instead of 0
Using inDegree[i] directly in the if condition without comparison
✗ Incorrect
Vertices with zero in-degree have no dependencies and can be processed first.
3fill in blank
hardFix the error in the loop that processes neighbors and updates in-degree.
DSA C#
while (queue.Count > 0) { int vertex = queue.Dequeue(); result.Add(vertex); foreach (int neighbor in graph[vertex]) { inDegree[[1]]--; if (inDegree[neighbor] == 0) { queue.Enqueue(neighbor); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Decreasing inDegree[vertex] instead of inDegree[neighbor]
Using unrelated variables like result.Count or queue.Count as index
✗ Incorrect
We decrease the in-degree of the neighbor because we have processed one of its incoming edges.
4fill in blank
hardFill both blanks to complete the function signature and return statement.
DSA C#
public static List<int> TopologicalSort([1] graph) { List<int> result = new List<int>(); // ... algorithm code ... return [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using int[] for graph parameter instead of List[]
Returning graph instead of result
✗ Incorrect
The graph is represented as an array of lists of integers, and the function returns the result list.
5fill in blank
hardFill all three blanks to complete the main loop that performs Kahn's algorithm.
DSA C#
while (queue.Count > 0) { int [1] = queue.Dequeue(); result.Add([2]); foreach (int neighbor in graph[[3]]) { inDegree[neighbor]--; if (inDegree[neighbor] == 0) { queue.Enqueue(neighbor); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in blanks
Using 'neighbor' instead of 'vertex' for indexing graph
✗ Incorrect
The variable 'vertex' is dequeued, added to result, and used to access neighbors.
