Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Adjacency Matrix Representation in DSA Javascript - Interactive Practice

Choose your learning style10 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 4 nodes.

DSA Javascript
const size = 4;
const adjacencyMatrix = Array(size).fill(null).map(() => Array(size).fill([1]));
Drag options to blanks, or click blank then click option'
Anull
B0
C1
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 initializes all edges as connected.
Using null or undefined can cause errors when checking edges.
2fill in blank
medium

Complete the code to add an edge from node 1 to node 3 in the adjacency matrix.

DSA Javascript
adjacencyMatrix[1][[1]] = 1;
Drag options to blanks, or click blank then click option'
A2
B3
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 instead of 2 (off-by-one error).
Confusing row and column indices.
3fill in blank
hard

Fix the error in the code to correctly check if there is an edge from node 2 to node 0.

DSA Javascript
if (adjacencyMatrix[[1]][0] === 1) {
  console.log('Edge exists');
}
Drag options to blanks, or click blank then click option'
A0
B3
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using node number directly without adjusting for zero-based index.
Mixing up row and column indices.
4fill in blank
hard

Fill both blanks to create a function that adds an undirected edge between two nodes in the adjacency matrix.

DSA Javascript
function addEdge(matrix, node1, node2) {
  matrix[node1][[1]] = 1;
  matrix[node2][[2]] = 1;
}
Drag options to blanks, or click blank then click option'
Anode2
Bnode1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting only one direction of the edge.
Mixing up node1 and node2 in assignments.
5fill in blank
hard

Fill all three blanks to create a function that returns all neighbors of a given node from the adjacency matrix.

DSA Javascript
function getNeighbors(matrix, node) {
  const neighbors = [];
  for (let i = 0; i < matrix[[1]].length; i++) {
    if (matrix[node][i] === [2]) {
      neighbors.push([3]);
    }
  }
  return neighbors;
}
Drag options to blanks, or click blank then click option'
Anode
B1
Ci
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Looping over wrong row or column.
Checking for 0 instead of 1.
Pushing wrong value instead of neighbor index.

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