Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the color slice with -1 for all vertices.
DSA Go
color := make([]int, len(graph)) for i := range color { color[i] = [1] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 1 as initial color causes confusion with actual colors.
✗ Incorrect
We use -1 to mark vertices as uncolored initially.
2fill in blank
mediumComplete the code to assign the opposite color to the neighbor vertex.
DSA Go
color[neighbor] = [1]Attempts:
3 left
💡 Hint
Common Mistakes
Adding or subtracting 1 can cause invalid colors like 2 or -1.
✗ Incorrect
We flip color 0 to 1 and 1 to 0 using 1 - current color.
3fill in blank
hardFix the error in the condition that checks if the neighbor has the same color as current.
DSA Go
if color[neighbor] [1] color[current] { return false }
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == causes wrong bipartite detection.
✗ Incorrect
If neighbor has the same color as current, graph is not bipartite.
4fill in blank
hardFill both blanks to complete the BFS loop that colors neighbors.
DSA Go
for len(queue) > 0 { current := queue[0] queue = queue[[1]:] for _, neighbor := range graph[current] { if color[neighbor] == -1 { color[neighbor] = [2] queue = append(queue, neighbor) } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using queue[0:] does not remove the first element.
Assigning same color to neighbor causes wrong bipartite check.
✗ Incorrect
We remove the first element with queue[1:] and assign opposite color to neighbor.
5fill in blank
hardFill all three blanks to complete the bipartite check function using BFS.
DSA Go
func isBipartite(graph [][]int) bool {
color := make([]int, len(graph))
for i := range color {
color[i] = [1]
}
for i := 0; i < len(graph); i++ {
if color[i] == -1 {
queue := []int{i}
color[i] = [2]
for len(queue) > 0 {
current := queue[0]
queue = queue[[3]:]
for _, neighbor := range graph[current] {
if color[neighbor] == -1 {
color[neighbor] = 1 - color[current]
queue = append(queue, neighbor)
} else if color[neighbor] == color[current] {
return false
}
}
}
}
}
return true
}Attempts:
3 left
💡 Hint
Common Mistakes
Initializing colors with 0 causes confusion with actual colors.
Removing queue element incorrectly causes infinite loops.
✗ Incorrect
Initialize colors with -1, start coloring with 0, and remove first queue element with queue[1:].
