Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the in-degree array for all vertices.
DSA Java
int[] inDegree = new int[V]; for (int i = 0; i < V; i++) { inDegree[i] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing in-degree with 1 or -1 instead of 0.
Using the number of vertices V as initial in-degree.
✗ Incorrect
In-degree counts start at zero for all vertices before counting edges.
2fill in blank
mediumComplete the code to increase the in-degree of adjacent vertices.
DSA Java
for (int u = 0; u < V; u++) { for (int v : adj.get(u)) { [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing in-degree of u instead of v.
Decreasing in-degree instead of increasing.
✗ Incorrect
For each edge u->v, increase in-degree of v by 1.
3fill in blank
hardFix the error in the queue initialization to add all vertices with zero in-degree.
DSA Java
Queue<Integer> queue = new LinkedList<>(); for (int i = 0; i < V; i++) { if (inDegree[i] [1] 0) { queue.add(i); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' or '<' instead of '==' causing wrong vertices to be added.
Using '>' which never matches zero in-degree.
✗ Incorrect
Vertices with in-degree equal to zero are added to the queue first.
4fill in blank
hardFill both blanks to correctly update in-degree and add vertices to the queue during BFS.
DSA Java
while (!queue.isEmpty()) { int u = queue.poll(); for (int v : adj.get(u)) { inDegree[[1]]--; if (inDegree[[2]] == 0) { queue.add(v); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using u instead of v for in-degree update.
Using queue or adj as index causing errors.
✗ Incorrect
Decrease in-degree of v and add v to queue if its in-degree becomes zero.
5fill in blank
hardFill all three blanks to complete the topological sort method returning the order list.
DSA Java
public List<Integer> topologicalSort(int V, List<List<Integer>> adj) {
int[] inDegree = new int[V];
for (int i = 0; i < V; i++) {
for (int v : adj.get(i)) {
inDegree[v][1];
}
}
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < V; i++) {
if (inDegree[i] [2] 0) {
queue.add(i);
}
}
List<Integer> topoOrder = new ArrayList<>();
while (!queue.isEmpty()) {
int u = queue.poll();
topoOrder.add(u);
for (int v : adj.get(u)) {
inDegree[v][3];
if (inDegree[v] == 0) {
queue.add(v);
}
}
}
return topoOrder;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using -- when counting in-degree initially.
Using != instead of == to check zero in-degree.
Using ++ when decreasing in-degree during BFS.
✗ Incorrect
Increase in-degree when counting edges, check for zero in-degree, decrease in-degree when removing edges.
