Write a Java program to rotate arrays 90 degree clockwise by taking matrices from user input.
Question Explanation
Rotating an array 90 degrees clockwise is a common problem in programming interviews that tests a candidate's understanding of array manipulation and matrix transformations. This question assesses not only your coding skills but also your ability to think algorithmically and optimize for performance. Interviewers often ask this question to evaluate how well candidates can handle multidimensional data structures, as well as their proficiency in Java. A thorough understanding of how to manipulate arrays is critical in many applications, such as game development and image processing. Moreover, this question can lead to discussions about space and time complexity, which are essential concepts in software engineering. Candidates should be prepared to explain their thought process and the rationale behind their chosen approach, including edge cases and potential optimizations. It's important to note that many candidates may struggle with the in-place rotation of matrices, leading to common pitfalls such as not considering the dimensions of the matrix or failing to handle non-square matrices. Overall, this question serves as a gateway to deeper discussions about algorithms and data structures in an interview setting.
Sample Answers
Example 1: In-Place Rotation
To rotate a matrix 90 degrees clockwise in Java, we can utilize an in-place rotation technique. This method involves two main steps: transposing the matrix and then reversing each row. Here's how you can implement it:
- Transpose the Matrix: Swap elements across the diagonal. For a matrix
matrix[i][j], swap it withmatrix[j][i]. - Reverse Each Row: After transposing, reverse each row to achieve the final rotated matrix.
Here’s the code:
public void rotate(int[][] matrix) {
int n = matrix.length;
// Step 1: Transpose the matrix
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// Step 2: Reverse each row
for (int i = 0; i < n; i++) {
for (int j = 0; j < n / 2; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[i][n - 1 - j];
matrix[i][n - 1 - j] = temp;
}
}
}
This approach has a time complexity of O(n^2) and a space complexity of O(1), making it efficient for rotating matrices.
Example 2: Using a New Matrix
An alternative approach to rotate a matrix 90 degrees clockwise is to create a new matrix. This method is straightforward but uses additional space. Here’s how you can do it:
- Create a New Matrix: Allocate a new matrix of the same size.
- Fill the New Matrix: Iterate through the original matrix and place each element in the new matrix at its rotated position.
Here’s the implementation:
public void rotate(int[][] matrix) {
int n = matrix.length;
int[][] rotated = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
rotated[j][n - 1 - i] = matrix[i][j];
}
}
// Copy back to original matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = rotated[i][j];
}
}
}
This method has a time complexity of O(n^2) and a space complexity of O(n^2) due to the additional matrix, which may not be optimal for large datasets but is easier to understand for beginners.
Example 3: Handling Non-Square Matrices
If you need to rotate a non-square matrix, the approach changes slightly because you cannot directly use the methods designed for square matrices. In this case, you will need to:
- Create a New Matrix: Allocate a new matrix with dimensions flipped (rows become columns and vice versa).
- Fill the New Matrix: Place elements according to the rotation logic.
Here’s how you can implement this:
public int[][] rotateNonSquare(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] rotated = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
rotated[j][rows - 1 - i] = matrix[i][j];
}
}
return rotated;
}
This function returns a new rotated matrix. The time complexity remains O(n * m) where n is the number of rows and m is the number of columns, and the space complexity is also O(n * m) for the new matrix. This method is essential when dealing with dynamic data structures, such as user-generated matrices.
Keywords
Ready to practice more questions?
Explore our collection of technical interview questions from top companies.
View All Questions