Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the distance array with infinity values.
DSA Typescript
const distances: number[] = new Array(graph.length).fill([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing distances with 0 causes incorrect shortest path calculations.
Using null or -1 can cause runtime errors when comparing distances.
✗ Incorrect
We initialize distances with Infinity to represent that nodes are initially unreachable.
2fill in blank
mediumComplete the code to update the distance for a neighbor node if a shorter path is found.
DSA Typescript
if (distances[current] + weight < distances[[1]]) { distances[[1]] = distances[current] + weight; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'current' instead of 'neighbor' causes wrong distance updates.
Using 'weight' or 'graph' as index causes runtime errors.
✗ Incorrect
We compare and update the distance of the neighbor node, not the current node.
3fill in blank
hardFix the error in the priority queue update to add the neighbor with its new distance.
DSA Typescript
priorityQueue.push({ node: [1], distance: distances[neighbor] }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing 'current' instead of 'neighbor' causes incorrect traversal order.
Using 'weight' or 'distances' as node causes errors.
✗ Incorrect
We must push the neighbor node with its updated distance into the priority queue.
4fill in blank
hardFill both blanks to correctly iterate over neighbors and extract their node and weight.
DSA Typescript
for (const [[1], [2]] of graph[current]) { // process neighbor and weight }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping neighbor and weight causes logic errors.
Using incorrect variable names leads to confusion.
✗ Incorrect
Each element in graph[current] is a pair [neighbor, weight], so we destructure accordingly.
5fill in blank
hardFill all three blanks to correctly initialize the distances, set the source distance, and start the priority queue.
DSA Typescript
const distances = new Array(graph.length).fill([1]); distances[[2]] = 0; priorityQueue.push({ node: [3], distance: 0 });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting all distances to 0 causes incorrect shortest path results.
Pushing wrong node into the priority queue delays processing.
✗ Incorrect
Distances start at Infinity, source node distance is 0, and we push the source node into the queue.