Data Structure 2D Arrays and Matrices Practice Questions with Solutions

Introductions

2D arrays, also called matrices, store data in rows and columns. They are commonly used for tables, grids, game boards, images, and mathematical problems. In this chapter, you will practice accessing matrix elements, traversing rows and columns, calculating sums, finding the largest value, searching elements, and performing basic matrix operations. The questions gradually move from simple indexing to practical matrix problems. Data Structure 2D Arrays and Matrices Practice questions with solutions help to understand the concepts.

Question 1: Access an Element from a 2D Array

Question

Given the following 2D array:

let matrix = [
    [10, 20, 30],
    [40, 50, 60],
    [70, 80, 90]
];

Find the value at row 1 and column 2.

Solution

A 2D array uses two indexes:

matrix[row][column]

The matrix is:

       Column
       0   1   2
     ┌───────────
Row 0│ 10  20  30
Row 1│ 40  50  60
Row 2│ 70  80  90

We need:

Row = 1
Column = 2

So:

console.log(matrix[1][2]);

Output

60

Answer

The value at row 1 and column 2 is 60.


Question 2: Print All Elements of a 2D Array

Question

Print every element of this matrix:

let matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

Solution

A 2D array has rows and columns, so we can use two loops.

The outer loop processes each row.

The inner loop processes each element inside that row.

let matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

for (let i = 0; i < matrix.length; i++) {
    for (let j = 0; j < matrix[i].length; j++) {
        console.log(matrix[i][j]);
    }
}

The traversal happens like this:

1 → 2 → 3
4 → 5 → 6
7 → 8 → 9

Output

1
2
3
4
5
6
7
8
9

Answer

The matrix is successfully traversed using nested loops.


Question 3: Find the Sum of All Matrix Elements

Question

Find the sum of all elements in this matrix:

let matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

Solution

Start with:

let sum = 0;

Now visit every element using nested loops.

let matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

let sum = 0;

for (let i = 0; i < matrix.length; i++) {
    for (let j = 0; j < matrix[i].length; j++) {
        sum += matrix[i][j];
    }
}

console.log(sum);

Let’s calculate:

1 + 2 + 3 = 6
4 + 5 + 6 = 15
7 + 8 + 9 = 24

Total:

6 + 15 + 24 = 45

Output

45

Answer

The sum of all matrix elements is 45.


Question 4: Find the Largest Element in a Matrix

Question

Find the largest element in this 2D array:

let matrix = [
    [12, 45, 7],
    [89, 23, 56],
    [34, 67, 15]
];

Solution

Start by assuming the first element is the largest:

let largest = matrix[0][0];

Initially:

largest = 12

Now compare every element.

let matrix = [
    [12, 45, 7],
    [89, 23, 56],
    [34, 67, 15]
];

let largest = matrix[0][0];

for (let i = 0; i < matrix.length; i++) {
    for (let j = 0; j < matrix[i].length; j++) {
        if (matrix[i][j] > largest) {
            largest = matrix[i][j];
        }
    }
}

console.log(largest);

Important comparisons include:

45 > 12 → largest = 45
89 > 45 → largest = 89
23 > 89 → No
56 > 89 → No
67 > 89 → No

No value is greater than 89.

Output

89

Answer

The largest element is 89.


Question 5: Find the Sum of Each Row

Question

Find the sum of every row in this matrix:

let matrix = [
    [10, 20, 30],
    [5, 15, 25],
    [2, 4, 6]
];

Solution

We need to calculate each row separately.

First row:

10 + 20 + 30 = 60

Second row:

5 + 15 + 25 = 45

Third row:

2 + 4 + 6 = 12

We can solve this using nested loops:

let matrix = [
    [10, 20, 30],
    [5, 15, 25],
    [2, 4, 6]
];

for (let i = 0; i < matrix.length; i++) {
    let rowSum = 0;

    for (let j = 0; j < matrix[i].length; j++) {
        rowSum += matrix[i][j];
    }

    console.log(rowSum);
}

Output

60
45
12

Answer

The row sums are:

Row 1 → 60
Row 2 → 45
Row 3 → 12

Question 6: Find the Sum of Each Column

Question

Find the sum of every column in this matrix:

let matrix = [
    [10, 20, 30],
    [5, 15, 25],
    [2, 4, 6]
];

Solution

The matrix is:

       C0  C1  C2
       ↓   ↓   ↓
       10  20  30
       5   15  25
       2   4   6

Column 0:

10 + 5 + 2 = 17

Column 1:

20 + 15 + 4 = 39

Column 2:

30 + 25 + 6 = 61

We can use the column index in the inner loop:

let matrix = [
    [10, 20, 30],
    [5, 15, 25],
    [2, 4, 6]
];

for (let j = 0; j < matrix[0].length; j++) {
    let columnSum = 0;

    for (let i = 0; i < matrix.length; i++) {
        columnSum += matrix[i][j];
    }

    console.log(columnSum);
}

Output

17
39
61

Answer

The column sums are:

Column 1 → 17
Column 2 → 39
Column 3 → 61

Question 7: Search for an Element in a Matrix

Question

Search for the number 50 in the following matrix and print its row and column:

let matrix = [
    [10, 20, 30],
    [40, 50, 60],
    [70, 80, 90]
];

Solution

We need to check every element.

When we find 50, we print its position.

let matrix = [
    [10, 20, 30],
    [40, 50, 60],
    [70, 80, 90]
];

let target = 50;

for (let i = 0; i < matrix.length; i++) {
    for (let j = 0; j < matrix[i].length; j++) {
        if (matrix[i][j] === target) {
            console.log("Found at row:", i, "column:", j);
        }
    }
}

The matrix is:

       C0  C1  C2
R0     10  20  30
R1     40  50  60
R2     70  80  90

The value 50 is located at:

Row = 1
Column = 1

Output

Found at row: 1 column: 1

Answer

The value 50 is located at row 1, column 1.


Question 8: Print the Main Diagonal

Question

Print the main diagonal elements of this matrix:

let matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

Solution

For a square matrix, the main diagonal contains elements where:

row index = column index

The matrix is:

1  2  3
4  5  6
7  8  9

The main diagonal is:

1
   5
      9

So we need:

matrix[0][0]
matrix[1][1]
matrix[2][2]

Code:

let matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

for (let i = 0; i < matrix.length; i++) {
    console.log(matrix[i][i]);
}

Output

1
5
9

Answer

The main diagonal elements are 1, 5, and 9.


Question 9: Transpose a Matrix

Question

Find the transpose of this matrix:

let matrix = [
    [1, 2, 3],
    [4, 5, 6]
];

Solution

The original matrix has 2 rows and 3 columns:

1  2  3
4  5  6

In a transpose:

Rows become columns.
Columns become rows.

Therefore:

1  4
2  5
3  6

We can create an empty result array:

let matrix = [
    [1, 2, 3],
    [4, 5, 6]
];

let transpose = [];

for (let j = 0; j < matrix[0].length; j++) {
    let row = [];

    for (let i = 0; i < matrix.length; i++) {
        row.push(matrix[i][j]);
    }

    transpose.push(row);
}

console.log(transpose);

The first column:

1
4

becomes the first row.

The second column:

2
5

becomes the second row.

The third column:

3
6

becomes the third row.

Output

[
    [1, 4],
    [2, 5],
    [3, 6]
]

Answer

The transpose is:

1  4
2  5
3  6

Question 10: Add Two Matrices

Question

Add the following two matrices:

let A = [
    [1, 2],
    [3, 4]
];

let B = [
    [5, 6],
    [7, 8]
];

Solution

To add two matrices, add elements at the same positions.

First position:

1 + 5 = 6

Second position:

2 + 6 = 8

Third position:

3 + 7 = 10

Fourth position:

4 + 8 = 12

So:

1  2       5  6
3  4   +   7  8

becomes:

6   8
10  12

JavaScript code:

let A = [
    [1, 2],
    [3, 4]
];

let B = [
    [5, 6],
    [7, 8]
];

let result = [];

for (let i = 0; i < A.length; i++) {
    let row = [];

    for (let j = 0; j < A[i].length; j++) {
        row.push(A[i][j] + B[i][j]);
    }

    result.push(row);
}

console.log(result);

Output

[
    [6, 8],
    [10, 12]
]

Answer

The resulting matrix is:

6   8
10  12

Key Takeaways

  • A 2D array stores data in rows and columns.
  • Access a value using matrix[row][column].
  • Both row and column indexes generally start from 0.
  • Nested loops are commonly used to traverse a 2D array.
  • The outer loop usually handles rows, while the inner loop handles columns.
  • You can calculate the total sum of all matrix elements using nested loops.
  • Row sums and column sums require careful control of the row and column indexes.
  • Searching a matrix usually involves checking each element.
  • The main diagonal of a square matrix contains elements where the row and column indexes are the same.
  • A matrix transpose changes rows into columns and columns into rows.
  • Matrix addition is performed by adding elements at corresponding positions.
  • Traversing every element of an m × n matrix generally takes O(m × n) time.

FAQs

What is a 2D array in data structures?

A 2D array is an array arranged in rows and columns. It can be used to represent tables, grids, matrices, game boards, and similar data.

How do I access an element in a 2D array?

Use two indexes:

matrix[row][column]

For example, matrix[1][2] accesses the element at row 1 and column 2.

Why are nested loops used with 2D arrays?

A 2D array has rows and columns. One loop can process the rows, while another loop processes the elements inside each row.

What is a matrix transpose?

A transpose changes the rows of a matrix into columns and the columns into rows.

What is the main diagonal of a matrix?

The main diagonal contains elements that have the same row and column index. For example, in a 3 × 3 matrix, these are positions [0][0], [1][1], and [2][2].

What is the time complexity of traversing a 2D array?

For a matrix containing m rows and n columns, visiting every element takes O(m × n) time.

Where are 2D arrays used in real programs?

2D arrays are commonly used for tables, spreadsheets, game boards, grids, image pixels, mathematical matrices, seating arrangements, and other row-and-column data.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top