Challenge - 5 Problems
Bipartite Graph Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Bipartite Check on Simple Graph
What is the output of the following TypeScript code that checks if a graph is bipartite?
DSA Typescript
function isBipartite(graph: number[][]): boolean {
const n = graph.length;
const colors = new Array(n).fill(-1);
for (let i = 0; i < n; i++) {
if (colors[i] === -1) {
const queue: number[] = [i];
colors[i] = 0;
while (queue.length) {
const node = queue.shift()!;
for (const neighbor of graph[node]) {
if (colors[neighbor] === -1) {
colors[neighbor] = 1 - colors[node];
queue.push(neighbor);
} else if (colors[neighbor] === colors[node]) {
return false;
}
}
}
}
}
return true;
}
const graph = [[1,3],[0,2],[1,3],[0,2]];
console.log(isBipartite(graph));Attempts:
2 left
💡 Hint
Think about whether the graph can be colored with two colors without conflicts.
✗ Incorrect
The graph is a square cycle (0-1-2-3-0). It can be colored with two colors alternating, so it is bipartite.
❓ Predict Output
intermediate2:00remaining
Output of Bipartite Check on Non-Bipartite Graph
What is the output of this TypeScript code that checks if a graph is bipartite?
DSA Typescript
function isBipartite(graph: number[][]): boolean {
const n = graph.length;
const colors = new Array(n).fill(-1);
for (let i = 0; i < n; i++) {
if (colors[i] === -1) {
const queue: number[] = [i];
colors[i] = 0;
while (queue.length) {
const node = queue.shift()!;
for (const neighbor of graph[node]) {
if (colors[neighbor] === -1) {
colors[neighbor] = 1 - colors[node];
queue.push(neighbor);
} else if (colors[neighbor] === colors[node]) {
return false;
}
}
}
}
}
return true;
}
const graph = [[1,2],[0,2],[0,1]];
console.log(isBipartite(graph));Attempts:
2 left
💡 Hint
Check if the graph contains an odd cycle.
✗ Incorrect
The graph is a triangle (0-1-2-0), which is an odd cycle and not bipartite.
🧠 Conceptual
advanced1:30remaining
Why BFS is used in Bipartite Check?
Why is Breadth-First Search (BFS) commonly used to check if a graph is bipartite?
Attempts:
2 left
💡 Hint
Think about how BFS explores nodes in layers.
✗ Incorrect
BFS explores nodes level by level, so it assigns colors alternately to neighbors, which helps detect conflicts in bipartite coloring.
🔧 Debug
advanced2:00remaining
Identify the Bug in Bipartite Check Code
What error will this TypeScript code produce when checking if a graph is bipartite?
DSA Typescript
function isBipartite(graph: number[][]): boolean {
const n = graph.length;
const colors = new Array(n).fill(-1);
for (let i = 0; i < n; i++) {
if (colors[i] === -1) {
const queue: number[] = [i];
colors[i] = 0;
while (queue.length) {
const node = queue.pop()!;
for (const neighbor of graph[node]) {
if (colors[neighbor] === -1) {
colors[neighbor] = 1 - colors[node];
queue.push(neighbor);
} else if (colors[neighbor] === colors[node]) {
return false;
}
}
}
}
}
return true;
}
const graph = [[1,3],[0,2],[1,3],[0,2]];
console.log(isBipartite(graph));Attempts:
2 left
💡 Hint
Check how nodes are removed from the queue.
✗ Incorrect
Using pop() makes the queue behave like a stack (DFS), which can cause incorrect coloring order and false negatives.
🚀 Application
expert3:00remaining
Number of Connected Components in Bipartite Graph
Given a graph represented as adjacency lists, how many connected components does the following graph have? Also, is it bipartite?
DSA Typescript
const graph = [ [1], // Node 0 connected to 1 [0], // Node 1 connected to 0 [3], // Node 2 connected to 3 [2], // Node 3 connected to 2 [] // Node 4 isolated ];
Attempts:
2 left
💡 Hint
Count isolated nodes and check if each component can be colored with two colors.
✗ Incorrect
The graph has three connected components: {0,1}, {2,3}, and {4} isolated. Each component is bipartite because they are simple edges or isolated nodes.