Spiral Matrix IV

MediumMatrixsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given two integers m and n, the number of rows and columns of a grid, and the head of a linked list of non-negative integers. Write the list's values into the grid in **clockwise spiral order**: start at the top-left cell moving right along the top row, turn down at the right edge, then left along the bottom, then up, spiraling inward and never revisiting a cell. Fill cells in that order until the list is exhausted. Any cells that receive no value hold -1. Return the m x n grid as a list of rows. The list never has more than m * n nodes.

Examples

Input: m = 3, n = 3, head = [1,2,3,4,5,6,7,8,9]
Output: [[1,2,3],[8,9,4],[7,6,5]]
Nine values fill the 3 x 3 grid in a full spiral ending at the center.
Input: m = 2, n = 3, head = [1,2,3,4]
Output: [[1,2,3],[-1,-1,4]]
1, 2, 3 fill the top row, 4 goes down the right column, and the last two cells stay -1.
Input: m = 1, n = 4, head = [0,1,2]
Output: [[0,1,2,-1]]
A single row is filled left to right.

Constraints

  • 1 <= m, n <= 10^5
  • 1 <= m * n <= 10^5
  • 1 <= number of nodes <= m * n
  • 0 <= Node.val <= 1000

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems