PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in in-place matrix and array manipulation, understanding of spatial transformations, and consideration of space and time complexity constraints.

  • medium
  • Apple
  • Coding & Algorithms
  • Software Engineer

Rotate a Matrix In Place

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given an `n x n` integer matrix, rotate it **in place** by 90 degrees clockwise. As follow-ups, explain how to modify the approach to rotate the same matrix: - 90 degrees counterclockwise - 180 degrees You should use `O(1)` extra space other than a few temporary variables. Example: Input: `[[1,2,3],[4,5,6],[7,8,9]]` After 90 degrees clockwise: `[[7,4,1],[8,5,2],[9,6,3]]` After 90 degrees counterclockwise: `[[3,6,9],[2,5,8],[1,4,7]]` After 180 degrees: `[[9,8,7],[6,5,4],[3,2,1]]`

Quick Answer: This question evaluates proficiency in in-place matrix and array manipulation, understanding of spatial transformations, and consideration of space and time complexity constraints.

Part 1: Rotate Matrix 90 Degrees Clockwise In Place

Given an n x n integer matrix `matrix`, rotate it 90 degrees clockwise. You must modify the matrix in place using only O(1) extra space other than a few temporary variables. For this problem, return the rotated matrix after modifying it.

Constraints

  • 0 <= n <= 200
  • matrix is square: matrix has n rows and each row has n columns
  • -10^9 <= matrix[i][j] <= 10^9

Examples

Input: []

Expected Output: []

Explanation: Empty matrix stays empty.

Input: [[42]]

Expected Output: [[42]]

Explanation: A 1x1 matrix is unchanged after rotation.

Input: [[1,2],[3,4]]

Expected Output: [[3,1],[4,2]]

Explanation: The top row becomes the right column.

Input: [[1,2,3],[4,5,6],[7,8,9]]

Expected Output: [[7,4,1],[8,5,2],[9,6,3]]

Explanation: Standard 3x3 clockwise rotation.

Input: [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]

Expected Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

Explanation: Works for larger square matrices too.

Hints

  1. Try breaking the rotation into two simpler in-place operations.
  2. After transposing a matrix, what row operation turns it into a clockwise rotation?

Part 2: Rotate Matrix 90 Degrees Counterclockwise In Place

Given an n x n integer matrix `matrix`, rotate it 90 degrees counterclockwise. You must modify the matrix in place using only O(1) extra space other than a few temporary variables. For this problem, return the rotated matrix after modifying it.

Constraints

  • 0 <= n <= 200
  • matrix is square: matrix has n rows and each row has n columns
  • -10^9 <= matrix[i][j] <= 10^9

Examples

Input: []

Expected Output: []

Explanation: Empty matrix stays empty.

Input: [[5]]

Expected Output: [[5]]

Explanation: A 1x1 matrix is unchanged after rotation.

Input: [[1,2],[3,4]]

Expected Output: [[2,4],[1,3]]

Explanation: Counterclockwise rotation moves the top row into the left column.

Input: [[1,2,3],[4,5,6],[7,8,9]]

Expected Output: [[3,6,9],[2,5,8],[1,4,7]]

Explanation: Standard 3x3 counterclockwise rotation.

Input: [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]

Expected Output: [[11,10,7,16],[9,8,6,12],[1,4,3,14],[5,2,13,15]]

Explanation: This demonstrates the in-place transformation on a 4x4 matrix.

Hints

  1. You can still use an in-place transpose as the first step.
  2. After transposing, decide whether reversing each row or reversing the order of the rows gives a left turn.

Part 3: Rotate Matrix 180 Degrees In Place

Given an n x n integer matrix `matrix`, rotate it 180 degrees. You must modify the matrix in place using only O(1) extra space other than a few temporary variables. For this problem, return the rotated matrix after modifying it.

Constraints

  • 0 <= n <= 200
  • matrix is square: matrix has n rows and each row has n columns
  • -10^9 <= matrix[i][j] <= 10^9

Examples

Input: []

Expected Output: []

Explanation: Empty matrix stays empty.

Input: [[9]]

Expected Output: [[9]]

Explanation: A 1x1 matrix is unchanged after rotation.

Input: [[1,2],[3,4]]

Expected Output: [[4,3],[2,1]]

Explanation: Every element moves to its opposite position.

Input: [[1,2,3],[4,5,6],[7,8,9]]

Expected Output: [[9,8,7],[6,5,4],[3,2,1]]

Explanation: Standard 3x3 180-degree rotation.

Input: [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]

Expected Output: [[16,12,14,15],[7,6,3,13],[10,8,4,2],[11,9,1,5]]

Explanation: Works for larger square matrices as well.

Hints

  1. A 180-degree rotation maps every element to the position symmetric through the center of the matrix.
  2. Can you achieve the result with two simple in-place reversals?
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Common Prefix Across Strings - Apple (easy)
  • Find Minimum Processing Rate - Apple
  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)
  • Encode and Rebuild a Binary Tree - Apple (hard)