0
0
DSA Typescriptprogramming~10 mins

Dijkstra's Algorithm Single Source Shortest Path in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A0
BInfinity
C-1
Dnull
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.
2fill in blank
medium

Complete 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'
Aneighbor
Bcurrent
Cweight
Dgraph
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'current' instead of 'neighbor' causes wrong distance updates.
Using 'weight' or 'graph' as index causes runtime errors.
3fill in blank
hard

Fix 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'
Aneighbor
Bcurrent
Cweight
Ddistances
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing 'current' instead of 'neighbor' causes incorrect traversal order.
Using 'weight' or 'distances' as node causes errors.
4fill in blank
hard

Fill 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'
Aneighbor
Bweight
Cnode
Ddistance
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping neighbor and weight causes logic errors.
Using incorrect variable names leads to confusion.
5fill in blank
hard

Fill 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'
AInfinity
Bsource
C0
Dstart
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.