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 this Rust code that checks if the given graph is bipartite?
DSA Rust
use std::collections::VecDeque; fn is_bipartite(graph: &Vec<Vec<usize>>) -> bool { let n = graph.len(); let mut colors = vec![-1; n]; for start in 0..n { if colors[start] == -1 { let mut queue = VecDeque::new(); queue.push_back(start); colors[start] = 0; while let Some(u) = queue.pop_front() { for &v in &graph[u] { if colors[v] == -1 { colors[v] = 1 - colors[u]; queue.push_back(v); } else if colors[v] == colors[u] { return false; } } } } } true } fn main() { let graph = vec![ vec![1, 3], vec![0, 2], vec![1, 3], vec![0, 2], ]; println!("{}", is_bipartite(&graph)); }
Attempts:
2 left
💡 Hint
Think about whether the graph can be colored with two colors without adjacent nodes sharing the same color.
✗ Incorrect
The graph is a square cycle (0-1-2-3-0), which is bipartite. The code colors nodes alternatingly and finds no conflict, so it returns true.
❓ Predict Output
intermediateOutput of Bipartite Check on Non-Bipartite Graph
What is the output of this Rust code that checks if the given graph is bipartite?
DSA Rust
use std::collections::VecDeque; fn is_bipartite(graph: &Vec<Vec<usize>>) -> bool { let n = graph.len(); let mut colors = vec![-1; n]; for start in 0..n { if colors[start] == -1 { let mut queue = VecDeque::new(); queue.push_back(start); colors[start] = 0; while let Some(u) = queue.pop_front() { for &v in &graph[u] { if colors[v] == -1 { colors[v] = 1 - colors[u]; queue.push_back(v); } else if colors[v] == colors[u] { return false; } } } } } true } fn main() { let graph = vec![ vec![1, 2], vec![0, 2], vec![0, 1], ]; println!("{}", is_bipartite(&graph)); }
Attempts:
2 left
💡 Hint
Check if the triangle graph can be colored with two colors without conflict.
✗ Incorrect
The graph is a triangle (3 nodes all connected), which is not bipartite. The code detects adjacent nodes with the same color and returns false.
🔧 Debug
advancedIdentify the Error in Bipartite Check Implementation
What error will this Rust code produce when checking if the graph is bipartite?
DSA Rust
use std::collections::VecDeque; fn is_bipartite(graph: &Vec<Vec<usize>>) -> bool { let n = graph.len(); let mut colors = vec![-1; n]; for start in 0..n { if colors[start] == -1 { let mut queue = VecDeque::new(); queue.push_back(start); colors[start] = 0; while let Some(u) = queue.pop_front() { for &v in &graph[u] { if colors[v] == -1 { colors[v] = 1 - colors[u]; queue.push_back(v); } else if colors[v] == colors[u] { return false; } } } } } true } fn main() { let graph = vec![ vec![1], vec![0], ]; println!("{}", is_bipartite(&graph)); }
Attempts:
2 left
💡 Hint
Look carefully at the line where colors[start] is assigned.
✗ Incorrect
The line 'colors[start] = 0' is missing a semicolon, causing a syntax error.
❓ Predict Output
advancedOutput of Bipartite Check on Disconnected Graph
What is the output of this Rust code that checks if the given disconnected graph is bipartite?
DSA Rust
use std::collections::VecDeque; fn is_bipartite(graph: &Vec<Vec<usize>>) -> bool { let n = graph.len(); let mut colors = vec![-1; n]; for start in 0..n { if colors[start] == -1 { let mut queue = VecDeque::new(); queue.push_back(start); colors[start] = 0; while let Some(u) = queue.pop_front() { for &v in &graph[u] { if colors[v] == -1 { colors[v] = 1 - colors[u]; queue.push_back(v); } else if colors[v] == colors[u] { return false; } } } } } true } fn main() { let graph = vec![ vec![1], vec![0], vec![3], vec![2, 4], vec![3], ]; println!("{}", is_bipartite(&graph)); }
Attempts:
2 left
💡 Hint
Consider each disconnected component separately for bipartite check.
✗ Incorrect
Both disconnected components are bipartite, so the function returns true.
❓ Predict Output
expertOutput of Bipartite Check on Complex Graph with Odd Cycle
What is the output of this Rust code that checks if the given complex graph with an odd cycle is bipartite?
DSA Rust
use std::collections::VecDeque; fn is_bipartite(graph: &Vec<Vec<usize>>) -> bool { let n = graph.len(); let mut colors = vec![-1; n]; for start in 0..n { if colors[start] == -1 { let mut queue = VecDeque::new(); queue.push_back(start); colors[start] = 0; while let Some(u) = queue.pop_front() { for &v in &graph[u] { if colors[v] == -1 { colors[v] = 1 - colors[u]; queue.push_back(v); } else if colors[v] == colors[u] { return false; } } } } } true } fn main() { let graph = vec![ vec![1, 4], vec![0, 2], vec![1, 3, 4], vec![2, 4], vec![0, 2, 3], ]; println!("{}", is_bipartite(&graph)); }
Attempts:
2 left
💡 Hint
Look for an odd cycle in the graph that prevents bipartite coloring.
✗ Incorrect
The graph contains an odd cycle (2-3-4-2), so it is not bipartite. The code detects color conflict and returns false.
