Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the visited set before BFS starts.
DSA Javascript
const visited = new [1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using an Array instead of a Set causes slower lookups.
Using an Object or Map is unnecessary for just tracking visited nodes.
✗ Incorrect
We use a Set to keep track of visited nodes because it allows fast membership checks.
2fill in blank
mediumComplete the code to add the starting node to the queue in BFS.
DSA Javascript
queue.push([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Adding the whole graph or visited set instead of a single node.
Adding neighbors before processing the start node.
✗ Incorrect
We add the startNode to the queue to begin BFS traversal from it.
3fill in blank
hardFix the error in the BFS loop condition to continue while queue is not empty.
DSA Javascript
while (queue.[1] > 0) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' which is for Sets or Maps, not arrays.
Using incorrect property names like 'count' or 'lengths'.
✗ Incorrect
Arrays in JavaScript use 'length' to get the number of elements.
4fill in blank
hardFill both blanks to check if a neighbor is unvisited and add it to the queue.
DSA Javascript
if (!visited.[1](neighbor)) { visited.[2](neighbor); queue.push(neighbor); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push' on a Set instead of 'add'.
Using 'includes' which is for arrays, not Sets.
✗ Incorrect
We check if neighbor is not visited using 'has', then mark it visited using 'add'.
5fill in blank
hardFill all three blanks to build the connected components list using BFS.
DSA Javascript
for (const node of Object.keys(graph)) { if (!visited.[1](node)) { const component = []; const queue = [node]; visited.[2](node); while (queue.[3] > 0) { const current = queue.shift(); component.push(current); for (const neighbor of graph[current]) { if (!visited.has(neighbor)) { visited.add(neighbor); queue.push(neighbor); } } } components.push(component); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' for array length.
Using 'push' instead of 'add' for Sets.
✗ Incorrect
We check if node is visited with 'has', mark it visited with 'add', and check queue length with 'length'.
