Challenge - 5 Problems
Bipartite Graph Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Bipartite Check on Simple Graph
What is the output of the following Go code that checks if a graph is bipartite?
DSA Go
package main import "fmt" func isBipartite(graph [][]int) bool { n := len(graph) colors := make([]int, n) for i := range colors { colors[i] = -1 } var dfs func(int, int) bool dfs = func(node, color int) bool { colors[node] = color for _, nei := range graph[node] { if colors[nei] == -1 { if !dfs(nei, 1-color) { return false } } else if colors[nei] == color { return false } } return true } for i := 0; i < n; i++ { if colors[i] == -1 { if !dfs(i, 0) { return false } } } return true } func main() { graph := [][]int{{1,3},{0,2},{1,3},{0,2}} fmt.Println(isBipartite(graph)) }
Attempts:
2 left
💡 Hint
Think about the graph edges and if nodes can be colored in two groups without conflict.
✗ Incorrect
The graph is a square cycle (0-1-2-3-0), which is bipartite. The code colors nodes alternatingly and finds no conflicts, so it returns true.
❓ Predict Output
intermediateOutput of Bipartite Check on Non-Bipartite Graph
What is the output of this Go code checking bipartite on a triangle graph?
DSA Go
package main import "fmt" func isBipartite(graph [][]int) bool { n := len(graph) colors := make([]int, n) for i := range colors { colors[i] = -1 } var dfs func(int, int) bool dfs = func(node, color int) bool { colors[node] = color for _, nei := range graph[node] { if colors[nei] == -1 { if !dfs(nei, 1-color) { return false } } else if colors[nei] == color { return false } } return true } for i := 0; i < n; i++ { if colors[i] == -1 { if !dfs(i, 0) { return false } } } return true } func main() { graph := [][]int{{1,2},{0,2},{0,1}} fmt.Println(isBipartite(graph)) }
Attempts:
2 left
💡 Hint
A triangle graph cannot be split into two groups without edges inside a group.
✗ Incorrect
The triangle graph has an odd cycle, so it is not bipartite. The code detects a conflict and returns false.
🔧 Debug
advancedIdentify the Error in Bipartite Check Implementation
What error will this Go code produce when run?
DSA Go
package main import "fmt" func isBipartite(graph [][]int) bool { n := len(graph) colors := make([]int, n) for i := range colors { colors[i] = -1 } var dfs func(int, int) bool dfs = func(node, color int) bool { colors[node] = color for _, nei := range graph[node] { if colors[nei] == -1 { if !dfs(nei, 1-color) { return false } } else if colors[nei] == color { return false } } return true } for i := 0; i <= n; i++ { if colors[i] == -1 { if !dfs(i, 0) { return false } } } return true } func main() { graph := [][]int{{1},{0}} fmt.Println(isBipartite(graph)) }
Attempts:
2 left
💡 Hint
Check the loop boundary in the for loop iterating over nodes.
✗ Incorrect
The loop uses i <= n which causes an index out of range panic because valid indices are 0 to n-1.
🧠 Conceptual
advancedWhich Graph Property Guarantees Bipartiteness?
Which property of a graph guarantees that it is bipartite?
Attempts:
2 left
💡 Hint
Think about cycle lengths and coloring nodes in two groups.
✗ Incorrect
A graph is bipartite if and only if it contains no odd length cycles. Even length cycles are allowed.
🚀 Application
expertResulting Color Groups After Bipartite Check
After running the bipartite check on this graph, what are the colors assigned to nodes? (0 or 1)
Graph adjacency list: [[1,4],[0,2],[1,3],[2,4],[0,3]]
Assume the algorithm colors node 0 with 0 first.
DSA Go
package main import "fmt" func isBipartiteColors(graph [][]int) ([]int, bool) { n := len(graph) colors := make([]int, n) for i := range colors { colors[i] = -1 } var dfs func(int, int) bool dfs = func(node, color int) bool { colors[node] = color for _, nei := range graph[node] { if colors[nei] == -1 { if !dfs(nei, 1-color) { return false } } else if colors[nei] == color { return false } } return true } for i := 0; i < n; i++ { if colors[i] == -1 { if !dfs(i, 0) { return nil, false } } } return colors, true } func main() { graph := [][]int{{1,4},{0,2},{1,3},{2,4},{0,3}} colors, ok := isBipartiteColors(graph) if ok { fmt.Println(colors) } else { fmt.Println("Not bipartite") } }
Attempts:
2 left
💡 Hint
Trace the DFS coloring starting from node 0 with color 0.
✗ Incorrect
Starting at node 0 with color 0, neighbors 1 and 4 get color 1, then 1's neighbor 2 gets 0, 2's neighbor 3 gets 1, and 4's neighbor 3 is already colored 1, no conflict.
