Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the colors array with -1 for all nodes.
DSA Javascript
const colors = new Array(graph.length).fill([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 1 as initial color instead of -1.
✗ Incorrect
We use -1 to mark nodes as uncolored initially.
2fill in blank
mediumComplete the code to assign the first color (0) to the starting node in BFS.
DSA Javascript
colors[start] = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning -1 or null instead of 0 to start node.
✗ Incorrect
We start coloring the first node with color 0.
3fill in blank
hardFix the error in the condition to check if adjacent node has the same color.
DSA Javascript
if (colors[neighbor] !== -1 && colors[neighbor] === [1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing neighbor's color with itself instead of current node's color.
✗ Incorrect
We compare neighbor's color with current node's color to detect conflict.
4fill in blank
hardFill both blanks to assign the opposite color to the neighbor node.
DSA Javascript
colors[neighbor] = 1 - [1]; queue.push([2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable for color or pushing wrong node to queue.
✗ Incorrect
Neighbor gets opposite color of current node; neighbor is added to queue.
5fill in blank
hardFill all three blanks to complete the BFS loop for bipartite check.
DSA Javascript
while (queue.length > 0) { const [1] = queue.shift(); for (const [2] of graph[[3]]) { if (colors[[2]] === -1) { colors[[2]] = colors[[1]] === 1 ? 0 : 1; queue.push([2]); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names causing confusion or errors.
✗ Incorrect
We dequeue current node, iterate neighbors, color uncolored neighbors, and enqueue them.
