Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add an edge from node u to node v in the adjacency list.
DSA Javascript
adjList[u].[1](v);Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of push() removes elements instead of adding.
Using shift() or unshift() changes the start of the list, not the end.
✗ Incorrect
To add an edge, we add the node v to the adjacency list of u using push().
2fill in blank
mediumComplete the code to initialize an empty adjacency list for n nodes.
DSA Javascript
const adjList = Array.from({ length: n }, () => [1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} creates an object, not an array.
Using null or 0 does not create a list to hold neighbors.
✗ Incorrect
Each node's adjacency list is an empty array to hold connected nodes.
3fill in blank
hardFix the error in the code to correctly add an undirected edge between u and v.
DSA Javascript
adjList[u].push(v);
adjList[[1]].push(u);Attempts:
3 left
💡 Hint
Common Mistakes
Using u instead of v causes adding u to its own list twice.
Using adjList or null causes runtime errors.
✗ Incorrect
For undirected edges, add v to u's list and u to v's list.
4fill in blank
hardFill both blanks to create an adjacency list from edges array.
DSA Javascript
for (const [[1], [2]] of edges) { adjList[u].push(v); adjList[v].push(u); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using node1 and node2 without updating inside loop causes errors.
Swapping u and v changes edge direction incorrectly.
✗ Incorrect
Edges are pairs [u, v]; we unpack them to variables u and v.
5fill in blank
hardFill all three blanks to print adjacency list connections.
DSA Javascript
for (let [1] = 0; [1] < adjList.length; [1]++) { console.log([2] + ' -> ' + adjList[[3]].join(', ')); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for printing and indexing causes errors.
Using undefined variables like node causes runtime errors.
✗ Incorrect
Use the loop variable i to access and print each node's adjacency list.
