Rotate a 3x3 grid of digits by 180 degrees
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Microsoft's coding round, last asked Jan 2026. It tests matrices and rotation.
You get a 3x3 grid of digits written as three strings of three characters each. Rotate it by 180 degrees and return the result in the same form. Validate the input: anything that is not exactly three rows of three digits is an error.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
rotate_180(grid: list[str]) -> list[str]— raisesValueErrorfor invalid input.
Rotating by 180 degrees sends the cell at (r, c) to (2 - r, 2 - c), which is the same as reversing the order of the rows and reversing each row.
Example
rotate_180(["123", "456", "789"]) -> ["987", "654", "321"]
rotate_180(["100", "020", "003"]) -> ["300", "020", "001"]
rotate_180(["12", "34", "56"]) -> ValueError # rows must have 3 characters
rotate_180(["12a", "456", "789"]) -> ValueError # not a digit
Constraints
- Exactly 3 rows of exactly 3 characters
0–9.
Clarifying questions
- What is the input format: strings, a list of lists of integers, or one 9-character string?
- Should invalid input raise, or return a sentinel?
- Should the rotation be in place?
- Do the digits themselves turn upside down (so
6becomes9), or only their positions?
What a strong answer covers
- The index mapping
(r, c) -> (2 - r, 2 - c)and its equivalence to reversing rows and each row. - An in-place version on a mutable grid: swap symmetric pairs, leaving the centre cell fixed.
- Input validation for row count, row length and characters.
- Generalising to
n x nand to 90-degree rotations (transpose, then reverse each row). - Tests: the centre stays put, a grid that is symmetric under rotation, invalid shapes and characters.
Follow-ups
- Also turn each digit upside down:
0,1and8stay,6and9swap, and any other digit makes the rotation invalid. - Rotate by 90 degrees clockwise, in place.
- Decide whether a grid looks the same after the 180-degree rotation.
Where it's asked
- Microsoft · Coding · Last asked Jan 202660 min · Ten to fifteen minutes of behavioral questions, then coding in HackerRank where you write a main and make the tests pass; usually three rounds · No AI unless the interviewer invites it; some 2026 loops add a round where you bring your own AI agent
What it tests
matrices · rotation
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
