Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the color array with -1 for all vertices.
DSA Python
color = [-1] * [1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using graph instead of its length causes a TypeError.
Using len(graph[0]) assumes graph is a matrix, but it's an adjacency list.
✗ Incorrect
We use len(graph) to get the number of vertices and initialize the color list with -1 for each vertex.
2fill in blank
mediumComplete the code to assign the starting vertex color as 0.
DSA Python
color[start] = [1]Attempts:
3 left
💡 Hint
Common Mistakes
Assigning -1 again means uncolored, so no progress.
Assigning None causes errors in comparisons.
✗ Incorrect
We assign color 0 to the starting vertex to begin the bipartite coloring.
3fill in blank
hardFix the error in the condition to check if the neighbor has the same color as the current vertex.
DSA Python
if color[neighbor] == [1]:
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing color[neighbor] to itself always true, causing wrong logic.
Comparing to -1 checks if uncolored, not conflict.
✗ Incorrect
We check if the neighbor's color is the same as the current node's color to detect a conflict.
4fill in blank
hardFill both blanks to assign the opposite color to the neighbor and add it to the queue.
DSA Python
color[neighbor] = 1 [1] color[node] queue.[2](neighbor)
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of - gives wrong color assignment.
Using pop instead of append removes elements instead of adding.
✗ Incorrect
We assign the opposite color by subtracting from 1 and add the neighbor to the queue using append.
5fill in blank
hardFill all three blanks to complete the BFS loop for bipartite check.
DSA Python
while queue: node = queue.[1]() for neighbor in graph[node]: if color[neighbor] == -1: color[neighbor] = 1 [2] color[node] queue.[3](neighbor)
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() removes from the end, breaking BFS order.
Using + instead of - changes color logic incorrectly.
✗ Incorrect
We use pop(0) to dequeue from the front, assign opposite color with -, and append neighbors to the queue.
