2 D Array

- Fundamentals -

Matrix Walk

1. Given a 2D array (matrix) of integers. Design an algorithm that:

  • Returns the elements in row-wise order (left to right, top to bottom).
  • Returns the elements in column-wise order (top to bottom, left to right).
Input: Matrix =[1, 2, 3],[4, 5, 6]Output:Row-Wise: [1, 2, 3, 4, 5, 6]Column-wise: [1,4, 2, 5, 3, 6]
Parameter\ Case Best CaseWorst CaseEdge Case
Parameters [[1, 2], [3, 4]]1000x1000 matrix with random numbers
[[5]] (1x1 matrix)
Row-wise[1, 2, 3, 4]
List of 1,000,000 numbers left-to-right, row-by-row
[5]
Column-Wise[1, 3, 2, 4]List of 1,000,000 numbers top-down, col-by-col[5]
Explanation Small matrix; easy traversal
Tests time/space complexity at scale
Single cell, both outputs same

Search in Unsorted Matrix

2. Given a 2D array (matrix) of integers and an integer target. Design an algorithm that determine whether target exists in the matrix.

The matrix is unsorted. The algorithm should return true if the element exists, otherwise return false.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters matrix = [[5, 2], [3, 4]], target = 5matrix = [[1, 2], [3, 4]], target = 9
matrix = [[10]], target = 10
Outputtrue
false
true
Explanation Target found immediately at first cell
Matrix is fully traversed, target not found
1x1 matrix, target is the only element

Boundary Traversal

3. Given a 2D matrix of dimensions m * n. Design an algorithm that return the boundary elements of the matrix in clockwise order, starting from the top left element.

The boundary includes:

  1. Top row (left to right)
  2. Rightmost column (top to bottom)
  3. Bottom row (right to left)
  4. Leftmost column (bottom to top)
Parameter\ Case Best CaseWorst CaseEdge Case
Parameters [1, 2, 3, 4],  [5, 6, 7, 8],  [9, 10, 11, 12]100x100 matrix
[[7]]
Output[1, 2, 3, 4, 8, 12, 11, 10, 9, 5]
396 elements (excluding repeated corners)
[7]
Explanation All 4 edges have only 1 row/col each
Traverses large matrix perimeter
Single cell is the boundary

Transpose Matrix

4. Given a 2D matrix of size m * n. Design an algorithm that return the transpose of the matrix.

The transpose of a matrix is formed by swapping its rows and columns, i.e., the element at position matrix[i][j] becomes matrix[j][i].

Perform the transpose in-place (without using extra space).

Otherwise, return a new matrix containing the transposed values.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters [[1, 2], [3, 4]]1000 x 500 matrix (rectangular, large)
[[7]], [[1, 2, 3]]
Output[[1, 3], [2, 4]]
A new 500 x 1000 transposed matrix
[[7]], [[1], [2], [3]]
Explanation Square matrix → in-place transpose
New matrix created with swapped dimensions
Smallest matrices still transpose correctly

Matrix Total Sum

5. Given a 2D array (matrix) of integers. Design an algorithm that compute and return the total sum of all elements in the matrix.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters [[1]]1000 × 1000 matrix of integers
[], [[]], or all-zero matrix
Output1
Sum of 1 million values (e.g., 500000000)
0 or error/empty sum if input is empty
Explanation Single cell; simplest computation
Must iterate through every element
Handles minimal or empty matrix correctly

- Intermediate Problems -

Search in Sorted Matrix

1. Given a 2D matrix of integers where each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom.

Given a target integer k. Design an algorithm that determine whether the target exists in the matrix.

Return true if it exists, otherwise return false.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters [[1, 2], [3, 4]], k = 11000×1000 sorted matrix, k = 999999
Empty matrix [] or single-element [[7]]
OutputTrue
False (after scanning entire diagonal path)
False or True
Explanation k is at top-left, found immediately
Full traversal from top-right to bottom-left needed before concluding False
Handles corner cases of empty input or small matrix correctly

Clockwise Matrix Walker

2. Given a 2D matrix of dimensions m * n. Design an algorithm that returns the elements of the matrix in spiral order, starting from top-left corner and moving clockwise.

In a spiral traversal, the matrix is read in layers:

  • Left to right (top row),
  • Top to bottom (right column),
  • Right to left (bottom row),
  • Bottom to top (left column), ...and so on, moving inward.

Return a list of elements in spiral order.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters [[1, 2, 3]] (1 row)[[1,2,3], [4,5,6], [7,8,9], ..., [997,998,999]]
[], [[]], or [[1]], or [[1], [2], [3]]
Output[1, 2, 3]
Very large spiral traversal (e.g. 1000×1000 matrix)
[], [1], or [1, 2, 3]
Explanation Single row — already spiral
Spiral through all elements layer by layer
Tests behavior with empty or minimal matrix input

Rotate Matrix 90 Degrees

3. Given an n * n square matrix of integers. Design an in-place algorithm to rotate the matrix 90 degrees clockwise.

Return the matrix after rotation.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters [[1]] or [[1, 2], [3, 4]]1000 x 1000 matrix with mixed integers
Single element [[5]] or empty []
Output[1, 2, 3]Matrix remains the same or rotates easily
Large rotated matrix with all values transformed
Same as input (single element), or error
Explanation Minimal or symmetric rotation
All layers must be swapped and transposed
Tests minimal case or handles input errors

Set Zeroes in Matrix

4. Given a 2D matrix of integers with dimensions m * n. Design an algorithm that modifies the matrix in-place such that if any element is 0, its entire row and column are set to 0.

Return the matrix after modification.

Parameter\ Case Best CaseWorst CaseEdge Case
Parameters No 0s present:
[[1, 2], [3, 4]]
Multiple 0s:
[[1, 0, 3], [0, 5, 6], [7, 8, 0]]
Single row:
[[1, 0, 3]]
Empty matrix: []
OutputMatrix remains unchanged:
[[1, 2], [3, 4]]
All affected rows and columns set to 0
Entire row/column zeroed or nothing changed
Explanation No zero values to propagate
Algorithm must track & zero correct rows/columns
Tests 1D matrix logic and boundary condition handling

Diagonal Pattern Traversal

5. Given a 2D matrix of integers with dimensions m * n. Design an algorithm that prints or returns the elements of the matrix in diagonal order.

In diagonal order, elements are grouped and traversed by their diagonals from top-left to bottom-right, meaning, All elements with the same sum of indices i + j are on the same diagonal.

Return a list of diagonals, where each diagonal is a list of elements.

[ 1, 2, 3 ][ 4, 5, 6 ][ 7, 8, 9 ][1],[2, 4],[3, 5, 7],[6, 8],[9]Diagonal order traversal// i + j = 0// i+ j = 1// i + j = 2// i + j = 3// i + j = 4
Parameter\ Case Best CaseWorst CaseEdge Case
Parameters [[1]], [[1, 2]], [[1], [2]]
Large matrix (e.g., 1000 x 1000 with random integers)
Empty matrix [], matrix with 1 row or 1 column
Output[[1]], [[1], [2]], [[1], [2]]
All diagonals grouped by i + j, each sublist containing many elements
[] or list with single-element sublists
Explanation Very few diagonals, easy to compute
Each element grouped by its i + j, and many diagonals present
Tests boundary behavior and correctness