Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the color array with -1 for all vertices.
DSA Java
int[] color = new int[V]; for (int i = 0; i < V; i++) { color[i] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 1 as initial color causes confusion with actual colors.
Not initializing the color array at all.
✗ Incorrect
We use -1 to indicate that a vertex has not been colored yet.
2fill in blank
mediumComplete the code to assign the starting vertex color as 0.
DSA Java
color[src] = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning -1 which means uncolored.
Assigning 1 instead of 0 arbitrarily.
✗ Incorrect
We start coloring the source vertex with color 0.
3fill in blank
hardFix the error in the condition to check if adjacent vertex has the same color.
DSA Java
if (color[adj] == [1]) { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with -1 which means uncolored.
Comparing with 0 instead of current vertex color.
✗ Incorrect
We check if the adjacent vertex has the same color as the current vertex, which means the graph is not bipartite.
4fill in blank
hardFill both blanks to assign the opposite color to the adjacent vertex and add it to the queue.
DSA Java
color[adj] = [1]; queue.add([2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the same color as current vertex.
Adding the current vertex instead of adjacent vertex to the queue.
✗ Incorrect
We assign the opposite color to the adjacent vertex and add the adjacent vertex to the queue for BFS.
5fill in blank
hardFill all three blanks to complete the BFS loop for checking bipartite graph.
DSA Java
while (!queue.isEmpty()) { int src = queue.poll(); for (int adj : graph[src]) { if (color[adj] == [1]) { color[adj] = [2]; queue.add([3]); } else if (color[adj] == color[src]) { return false; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for color 0 instead of -1 for uncolored.
Adding current vertex instead of adjacent vertex to queue.
✗ Incorrect
We check if adjacent vertex is uncolored (-1), assign opposite color, and add adjacent vertex to queue.
