Rotate a Square Grid Clockwise
Updated
Problem statement
You are given a square grid of integers, grid, with n rows and n columns.
Turn the grid a quarter turn clockwise (90 degrees to the right) and return the turned grid. After the turn, the first column read from bottom to top becomes the first row, the second column read from bottom to top becomes the second row, and so on.
You may build a new grid or change grid itself and return it. Doing it in place, with only a few extra variables, is a good follow-up.
Function
rotateClockwise(grid: int[][]) → int[][]Examples
Example 1
grid = [[6,1],[3,8]]return = [[3,6],[8,1]]Turning the grid a quarter turn to the right moves the left column, read from bottom to top (3, 6), into the top row. The right column, read from bottom to top (8, 1), becomes the bottom row.
Example 2
grid = [[5,-1,0],[7,8,2],[-4,6,9]]return = [[-4,7,5],[6,8,-1],[9,2,0]]The first column read from bottom to top is -4, 7, 5, so that is the new first row. The second column from bottom to top is 6, 8, -1, and the third is 9, 2, 0. The center value 8 stays in place.
Example 3
grid = [[-7]]return = [[-7]]A grid with one cell looks the same after any turn.
Constraints
1 <= n <= 25, wheren == grid.length == grid[i].length-1000 <= grid[i][j] <= 1000
More C3.ai interview questions
- Every Result of Grouping an ExpressionONSITE INTERVIEW · Seen Jan 2023
- Fewest Jumps to the Last IndexONSITE INTERVIEW · Seen Jan 2023
- Movie Schedule for Maximum EnjoymentONSITE INTERVIEW · Seen Jan 2023
- Days Until a Warmer DayPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Can All Courses Be FinishedONSITE INTERVIEW · Seen Nov 2021
- Longest Arithmetic SubsequenceONSITE INTERVIEW · Seen Feb 2021
- Gas Station LoopONSITE INTERVIEW · Seen Dec 2020
- Drop Recent Duplicate MessagesOA · Seen Oct 2026
- Maximum K-Star SumOA · Seen Oct 2026
- Fewest Turns to Open a Wheel LockPHONE SCREEN · Seen Nov 2024
Similar questions at other companies
- Optimal Central WarehouseGoogle · ONSITE INTERVIEWGoogle interview questions
- Linear Layer Forward and Backward PassesMeta · ONSITE INTERVIEWMeta interview questions
- Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
- Tensor-Parallel MLP GradientsOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Build a Gaussian Blur KernelLuma AI · OALuma AI interview questions
- Linear, Sigmoid and L2 Forward and Backward PassesNVIDIA · PHONE SCREENNVIDIA interview questions
- Rotate a Square Matrix 90 Degrees ClockwiseZipRecruiter · OAZipRecruiter interview questions
- Rotate ImageTokopedia · PHONE SCREENTokopedia interview questions
- Rotate ImageSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
- Survey Table: Group Given an AnswerSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions