Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an adjacency list for a graph with 3 nodes.
DSA Typescript
const graph: number[][] = [[], [], []]; graph[0].[1](1); graph[1].push(2); console.log(graph);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'append' which is not a method in TypeScript arrays.
✗ Incorrect
In TypeScript arrays, the method to add an element at the end is 'push'.
2fill in blank
mediumComplete the code to initialize an adjacency matrix for a graph with 3 nodes.
DSA Typescript
const size = 3; const matrix: number[][] = Array(size).fill(0).map(() => Array(size).[1](0)); console.log(matrix);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push' which adds elements instead of filling existing ones.
✗ Incorrect
To create an array filled with zeros, use the 'fill' method.
3fill in blank
hardFix the error in the code to add an edge in the adjacency matrix.
DSA Typescript
const matrix = [[0, 0], [0, 0]]; matrix[0][[1]] = 1; console.log(matrix);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 which points to the same node, not the edge.
✗ Incorrect
To add an edge from node 0 to node 1, set matrix[0][1] = 1.
4fill in blank
hardFill both blanks to create an adjacency list and add an edge from node 2 to node 0.
DSA Typescript
const graph: number[][] = [[], [], []]; graph[[1]].[2](0); console.log(graph);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'append' which is not a TypeScript array method.
✗ Incorrect
To add an edge from node 2 to node 0, use graph[2].push(0).
5fill in blank
hardFill all three blanks to create an adjacency matrix and add an edge from node 1 to node 2.
DSA Typescript
const size = 3; const matrix: number[][] = Array(size).fill(0).map(() => Array(size).[1](0)); matrix[[2]][[3]] = 1; console.log(matrix);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices or methods that don't exist.
✗ Incorrect
Initialize the matrix with 'fill', then set matrix[1][2] = 1 to add the edge.