0
0
DSA Typescriptprogramming~20 mins

Bipartite Graph Check in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bipartite Graph Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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));
ASyntaxError
Bfalse
CTypeError
Dtrue
Attempts:
2 left
💡 Hint
Think about whether the graph can be colored with two colors without conflicts.
Predict Output
intermediate
2: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));
ARangeError
Bfalse
CReferenceError
Dtrue
Attempts:
2 left
💡 Hint
Check if the graph contains an odd cycle.
🧠 Conceptual
advanced
1:30remaining
Why BFS is used in Bipartite Check?
Why is Breadth-First Search (BFS) commonly used to check if a graph is bipartite?
ABecause BFS finds shortest paths which are needed to detect cycles.
BBecause BFS uses less memory than DFS in all cases.
CBecause BFS colors nodes level by level, ensuring neighbors get opposite colors.
DBecause BFS can only be used on bipartite graphs.
Attempts:
2 left
💡 Hint
Think about how BFS explores nodes in layers.
🔧 Debug
advanced
2: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));
AThe code returns false incorrectly due to using stack behavior instead of queue.
BThe code throws a TypeError because of wrong array method.
CThe code returns true correctly.
DThe code causes an infinite loop.
Attempts:
2 left
💡 Hint
Check how nodes are removed from the queue.
🚀 Application
expert
3: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
];
A3 connected components, graph is bipartite
B2 connected components, graph is bipartite
C3 connected components, graph is not bipartite
D2 connected components, graph is not bipartite
Attempts:
2 left
💡 Hint
Count isolated nodes and check if each component can be colored with two colors.