0
0
DSA Typescriptprogramming~20 mins

Adjacency Matrix Representation in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Adjacency Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of adjacency matrix after adding edges
What is the printed adjacency matrix after adding edges between nodes 0-1, 1-2, and 2-0 in an undirected graph of 3 nodes?
DSA Typescript
class Graph {
  matrix: number[][];
  size: number;
  constructor(size: number) {
    this.size = size;
    this.matrix = Array.from({ length: size }, () => Array(size).fill(0));
  }
  addEdge(u: number, v: number) {
    this.matrix[u][v] = 1;
    this.matrix[v][u] = 1;
  }
  printMatrix() {
    for (let i = 0; i < this.size; i++) {
      console.log(this.matrix[i].join(' '));
    }
  }
}

const g = new Graph(3);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.printMatrix();
A
0 0 0
0 0 0
0 0 0
B
0 1 0
1 0 1
0 1 0
C
1 1 1
1 1 1
1 1 1
D
0 1 1
1 0 1
1 1 0
Attempts:
2 left
💡 Hint
Think about how undirected edges are represented symmetrically in the matrix.
Predict Output
intermediate
2:00remaining
Output after removing an edge in adjacency matrix
What is the adjacency matrix output after removing the edge between nodes 1 and 2 from the previous graph?
DSA Typescript
class Graph {
  matrix: number[][];
  size: number;
  constructor(size: number) {
    this.size = size;
    this.matrix = Array.from({ length: size }, () => Array(size).fill(0));
  }
  addEdge(u: number, v: number) {
    this.matrix[u][v] = 1;
    this.matrix[v][u] = 1;
  }
  removeEdge(u: number, v: number) {
    this.matrix[u][v] = 0;
    this.matrix[v][u] = 0;
  }
  printMatrix() {
    for (let i = 0; i < this.size; i++) {
      console.log(this.matrix[i].join(' '));
    }
  }
}

const g = new Graph(3);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.removeEdge(1, 2);
g.printMatrix();
A
0 0 0
0 0 0
0 0 0
B
0 1 1
1 0 1
1 1 0
C
0 1 1
1 0 0
1 0 0
D
1 1 1
1 1 1
1 1 1
Attempts:
2 left
💡 Hint
Removing an edge sets the corresponding matrix entries to zero.
🔧 Debug
advanced
2:00remaining
Identify the error in adjacency matrix initialization
What error will this code produce when creating an adjacency matrix for 3 nodes?
DSA Typescript
class Graph {
  matrix: number[][];
  size: number;
  constructor(size: number) {
    this.size = size;
    this.matrix = Array(size).fill(Array(size).fill(0));
  }
}

const g = new Graph(3);
g.matrix[0][1] = 1;
console.log(g.matrix);
AAll rows change when one element is updated because inner arrays are shared
BTypeError because matrix is undefined
CSyntaxError due to missing semicolon
DNo error, matrix updates correctly
Attempts:
2 left
💡 Hint
Check how Array.fill works with objects inside.
Predict Output
advanced
2:00remaining
Output of adjacency matrix for directed graph
What is the adjacency matrix output after adding directed edges 0->1, 1->2, and 2->0 in a graph of 3 nodes?
DSA Typescript
class DirectedGraph {
  matrix: number[][];
  size: number;
  constructor(size: number) {
    this.size = size;
    this.matrix = Array.from({ length: size }, () => Array(size).fill(0));
  }
  addEdge(u: number, v: number) {
    this.matrix[u][v] = 1;
  }
  printMatrix() {
    for (let i = 0; i < this.size; i++) {
      console.log(this.matrix[i].join(' '));
    }
  }
}

const g = new DirectedGraph(3);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.printMatrix();
A
0 1 0
0 0 1
1 0 0
B
0 1 1
1 0 1
1 1 0
C
0 0 0
0 0 0
0 0 0
D
1 0 0
0 1 0
0 0 1
Attempts:
2 left
💡 Hint
Directed edges only set one direction in the matrix.
🧠 Conceptual
expert
2:00remaining
Memory complexity of adjacency matrix vs adjacency list
Which statement correctly compares the memory usage of adjacency matrix and adjacency list for a graph with V vertices and E edges?
ABoth adjacency matrix and adjacency list use O(V^2) memory
BAdjacency matrix uses O(V^2) memory regardless of edges; adjacency list uses O(V + E) memory
CAdjacency matrix uses O(E) memory; adjacency list uses O(V) memory
DAdjacency matrix uses O(V + E) memory; adjacency list uses O(V^2) memory
Attempts:
2 left
💡 Hint
Think about how adjacency matrix stores all possible edges.