0
0
DSA Typescriptprogramming~10 mins

Adjacency Matrix Representation 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 an adjacency matrix for a graph with n vertices.

DSA Typescript
const adjacencyMatrix: number[][] = Array.from({ length: n }, () => Array(n).fill([1]));
Drag options to blanks, or click blank then click option'
A0
B1
C-1
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 will incorrectly show edges where there are none.
Using null or -1 is not standard for adjacency matrices.
2fill in blank
medium

Complete the code to add an edge from vertex u to vertex v in the adjacency matrix.

DSA Typescript
adjacencyMatrix[[1]][v] = 1;
Drag options to blanks, or click blank then click option'
Av
Bu
C0
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping u and v will add the edge in the wrong direction.
Using 0 or n as index will cause errors.
3fill in blank
hard

Fix the error in the code that checks if there is an edge from u to v.

DSA Typescript
if (adjacencyMatrix[u][[1]] === 1) { console.log('Edge exists'); }
Drag options to blanks, or click blank then click option'
Av
Bu
C0
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using u for both indices checks a self-loop incorrectly.
Using 0 or n as index causes out-of-bounds errors.
4fill in blank
hard

Fill both blanks to create a function that returns true if there is an edge from source to target.

DSA Typescript
function hasEdge(adjacencyMatrix: number[][], source: number, target: number): boolean {
  return adjacencyMatrix[[1]][[2]] === 1;
}
Drag options to blanks, or click blank then click option'
Asource
Btarget
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping source and target indices will check the wrong edge.
Using incorrect variable names causes errors.
5fill in blank
hard

Fill all three blanks to create a function that builds an adjacency matrix from a list of edges.

DSA Typescript
function buildAdjacencyMatrix(n: number, edges: [number, number][]): number[][] {
  const matrix = Array.from({ length: n }, () => Array(n).fill(0));
  for (const [[1], [2]] of edges) {
    matrix[[3]][[2]] = 1;
  }
  return matrix;
}
Drag options to blanks, or click blank then click option'
Au
Bv
Attempts:
3 left
💡 Hint
Common Mistakes
Using v as the row index and u as the column index swaps edge direction.
Not destructuring edges properly causes errors.