Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Adjacency Matrix Representation in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 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 running the following code that adds edges to a graph with 3 nodes?
DSA Javascript
class Graph {
  constructor(size) {
    this.size = size;
    this.matrix = Array.from({ length: size }, () => Array(size).fill(0));
  }
  addEdge(u, v) {
    this.matrix[u][v] = 1;
    this.matrix[v][u] = 1;
  }
  printMatrix() {
    for (let row of this.matrix) {
      console.log(row.join(' '));
    }
  }
}

const g = new Graph(3);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.printMatrix();
A
0 1 0
1 0 1
0 1 0
B
0 0 0
0 0 0
0 0 0
C
0 1 1
1 0 1
1 1 0
D
1 1 0
1 1 1
0 1 1
Attempts:
2 left
💡 Hint
Remember that addEdge marks both [u][v] and [v][u] as 1 for an undirected graph.
❓ Predict Output
intermediate
2:00remaining
Adjacency matrix after removing an edge
What is the output adjacency matrix after removing the edge between nodes 1 and 2 in the following code?
DSA Javascript
class Graph {
  constructor(size) {
    this.size = size;
    this.matrix = Array.from({ length: size }, () => Array(size).fill(0));
  }
  addEdge(u, v) {
    this.matrix[u][v] = 1;
    this.matrix[v][u] = 1;
  }
  removeEdge(u, v) {
    this.matrix[u][v] = 0;
    this.matrix[v][u] = 0;
  }
  printMatrix() {
    for (let row of this.matrix) {
      console.log(row.join(' '));
    }
  }
}

const g = new Graph(3);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.removeEdge(1, 2);
g.printMatrix();
A
0 1 0
1 0 0
0 0 0
B
0 1 0
1 0 1
0 1 0
C
0 0 0
0 0 0
0 0 0
D
1 1 0
1 1 1
0 1 1
Attempts:
2 left
💡 Hint
Removing edge sets the corresponding entries to 0.
🧠 Conceptual
advanced
2:00remaining
Memory usage of adjacency matrix vs adjacency list
Which statement correctly describes the memory usage difference between adjacency matrix and adjacency list for a graph with V vertices and E edges?
AAdjacency matrix uses O(V + E) memory; adjacency list uses O(V^2) memory.
BBoth adjacency matrix and adjacency list use O(V^2) memory.
CAdjacency matrix always uses O(V^2) memory regardless of edges; adjacency list uses O(V + E) memory.
DAdjacency matrix uses O(E) memory; adjacency list uses O(V) memory.
Attempts:
2 left
💡 Hint
Think about how adjacency matrix stores all possible edges.
❓ Predict Output
advanced
2:00remaining
Output after checking edge existence
What is the output of the following code that checks if an edge exists between nodes 0 and 2?
DSA Javascript
class Graph {
  constructor(size) {
    this.size = size;
    this.matrix = Array.from({ length: size }, () => Array(size).fill(0));
  }
  addEdge(u, v) {
    this.matrix[u][v] = 1;
    this.matrix[v][u] = 1;
  }
  hasEdge(u, v) {
    return this.matrix[u][v] === 1;
  }
}

const g = new Graph(3);
g.addEdge(0, 1);
g.addEdge(1, 2);
console.log(g.hasEdge(0, 2));
Atrue
Bfalse
Cundefined
DTypeError
Attempts:
2 left
💡 Hint
Edge 0-2 was never added.
🧠 Conceptual
expert
2:00remaining
Time complexity of checking edge existence in adjacency matrix
What is the time complexity to check if an edge exists between two nodes in an adjacency matrix representation of a graph with V vertices?
AO(log V) logarithmic time
BO(V) linear time
CO(E) proportional to edges
DO(1) constant time
Attempts:
2 left
💡 Hint
Adjacency matrix stores direct access to edges.

Practice

(1/5)
1. What does an adjacency matrix represent in a graph?
easy
A. A 2D array showing connections between nodes
B. A list of nodes in the graph
C. A sequence of edges in order
D. A tree structure of nodes

Solution

  1. Step 1: Understand adjacency matrix structure

    An adjacency matrix is a 2D array where rows and columns represent nodes.
  2. Step 2: Identify what the matrix stores

    Each cell shows if there is a connection (edge) between two nodes.
  3. Final Answer:

    A 2D array showing connections between nodes -> Option A
  4. Quick Check:

    Adjacency matrix = 2D array of connections [OK]
Hint: Adjacency matrix is always a 2D array of node links [OK]
Common Mistakes:
  • Confusing adjacency matrix with adjacency list
  • Thinking it stores node values instead of connections
  • Assuming it is a sequence or tree
2. Which of the following is the correct way to declare a 3x3 adjacency matrix in JavaScript?
easy
A. const matrix = [0,0,0];
B. const matrix = [[0,0,0],[0,0,0],[0,0,0]];
C. const matrix = new Array(3);
D. const matrix = {0:0,1:0,2:0};

Solution

  1. Step 1: Understand adjacency matrix as 2D array

    It must be an array of arrays, each inner array representing a row.
  2. Step 2: Check each option's structure

    const matrix = [[0,0,0],[0,0,0],[0,0,0]]; is a 3x3 array filled with zeros, correct for adjacency matrix initialization.
  3. Final Answer:

    const matrix = [[0,0,0],[0,0,0],[0,0,0]]; -> Option B
  4. Quick Check:

    2D array syntax = const matrix = [[0,0,0],[0,0,0],[0,0,0]]; [OK]
Hint: Adjacency matrix needs nested arrays for rows and columns [OK]
Common Mistakes:
  • Using a single array instead of 2D array
  • Using object instead of array
  • Creating array without inner arrays
3. What is the output of the following code?
const matrix = [
  [0, 1, 0],
  [1, 0, 1],
  [0, 1, 0]
];
console.log(matrix[1][2]);
medium
A. 1
B. 0
C. undefined
D. Error

Solution

  1. Step 1: Identify indices in matrix

    matrix[1] is the second row: [1, 0, 1].
  2. Step 2: Access element at column 2

    matrix[1][2] is the third element in second row, which is 1.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    matrix[1][2] = 1 [OK]
Hint: Remember matrix[row][column] indexing starts at 0 [OK]
Common Mistakes:
  • Confusing row and column indices
  • Assuming 1-based indexing
  • Expecting undefined for valid indices
4. Find the error in this adjacency matrix update code:
const matrix = [
  [0, 0],
  [0, 0]
];
matrix[0][1] = 1;
matrix[1][2] = 1;
medium
A. Matrix should be 1D array
B. Cannot assign value to matrix[0][1]
C. Index 2 is out of bounds for matrix[1]
D. No error, code is correct

Solution

  1. Step 1: Check matrix dimensions

    Matrix has 2 rows and 2 columns (indices 0 and 1 only).
  2. Step 2: Identify invalid index access

    matrix[1][2] tries to access third element in second row, which does not exist.
  3. Final Answer:

    Index 2 is out of bounds for matrix[1] -> Option C
  4. Quick Check:

    Accessing invalid column index = Error [OK]
Hint: Check array length before accessing indices [OK]
Common Mistakes:
  • Ignoring array size limits
  • Assuming dynamic resizing on assignment
  • Confusing row and column indices
5. Given a graph with 4 nodes, how would you represent an undirected edge between node 1 and node 3 in an adjacency matrix?
hard
A. Set matrix[1][1] = 1 and matrix[3][3] = 1
B. Set matrix[1][3] = 1 only
C. Set matrix[3][1] = 1 only
D. Set matrix[1][3] = 1 and matrix[3][1] = 1

Solution

  1. Step 1: Understand undirected edge representation

    Undirected edges are symmetric, so both directions must be marked.
  2. Step 2: Mark both connections in matrix

    Set matrix[1][3] = 1 and matrix[3][1] = 1 to show connection both ways.
  3. Final Answer:

    Set matrix[1][3] = 1 and matrix[3][1] = 1 -> Option D
  4. Quick Check:

    Undirected edge = symmetric matrix entries [OK]
Hint: Undirected edges need two symmetric entries set [OK]
Common Mistakes:
  • Setting only one direction for undirected edge
  • Setting diagonal elements instead of edge
  • Confusing node indices
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard