Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
O(m*n)
, m being the rows of the matrix and n being the columns of matrix. Space complexity should be O(1)
.HAPPY CASE
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
EDGE CASE
Input: matrix = [[1]]
Output: [[1]]
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For 2D-Array, common solution patterns include:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Let's recursively rotate each item layer by layer.
1) Create a helper function with the boundary for rotation
a)Basecase: Single square
b)Rotate the items from upper-row with left-column, left-column with bottom-row, bottom-row with right-column, and right-column with upper-row
i) Store upper-row
ii) Set upper-row with value in left-column
iii) Set left-column with value in bottom-row
iv) Set bottom-row with value in right-column
v) Set the right-column with upper-row
c)Recursive reduce the layers
2) Run helper function starting at the outermost layer
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"
Do not return anything, modify matrix in-place instead.
"
def helper(top_left, bottom_right):
# Basecase: Single square
if bottom_right <= top_left:
return
# Rotate the items from upper-row with left-column, left-column with bottom-row, bottom-row with right-column, and right-column with upper-row
for i in range(top_left, bottom_right):
tmp = matrix[top_left][i] # Store upper-row
matrix[top_left][i] = matrix[n-i][top_left] # Set upper-row with value in left-column
matrix[n-i][top_left] = matrix[n-top_left][n-i] # Set left-column with value in bottom-row
matrix[n-top_left][n-i] = matrix[n-(n-i)][n-top_left] # Set bottom-row with value in right-column
matrix[n-(n-i)][n-top_left] = tmp # Set the right-column with upper-row
# Recursive reduce the layers
helper(top_left+1, bottom_right-1)
# Run helper function starting at the outermost layer
n = len(matrix)-1
helper(0, n)
class Solution {
public void rotate(int[][] matrix) {
// Run helper function starting at the outermost layer
int n = matrix.length;
solve(matrix, n, 0);
return;
}
void solve(int[][] matrix, int n, int start){
// Basecase: Center square
if(start >= n/2){
return;
}
// Rotate the items from upper-row with left-column, left-column with bottom-row, bottom-row with right-column, and right-column with upper-row
for(int i = start; i < n-1-start; i++){
int a = matrix[start][i];
int b = matrix[i][n-1-start];
int c = matrix[n-1-start][n-i-1];
int d = matrix[n-i-1][start];
matrix[start][i] = d;
matrix[i][n-1-start] = a;
matrix[n-1-start][n-i-1] = b;
matrix[n-i-1][start] = c;
}
// Recursive reduce the layers
solve(matrix, n, start+1);
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of rows in 2D-array.
Assume M
represents the number of columns in 2D-array.