Reshape the Matrix

EasyMatrixsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an m x n matrix mat and two integers r and c, reshape it into an r x c matrix that holds the same elements in the same row-by-row reading order. If r * c does not equal m * n, the reshape is impossible; return the original matrix unchanged.

Examples

Input: mat = [[1,2],[3,4]], r = 1, c = 4
Output: [[1,2,3,4]]
Reading 1, 2, 3, 4 and writing them into one row of 4.
Input: mat = [[1,2],[3,4]], r = 2, c = 4
Output: [[1,2],[3,4]]
2 x 4 = 8 cells but only 4 elements exist, so `mat` is returned as-is.

Constraints

  • m == mat.length, n == mat[i].length
  • 1 <= m, n <= 100
  • -1000 <= mat[i][j] <= 1000
  • 1 <= r, c <= 300

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems