Member-only story
Spiral 🌀 Matrix II | Daily LeetCode Challenge | Day 13 | Coding Interview
3 min readApr 13, 2022
This problem is very similar to the Spiral Matrix I, there we had to print the matrix in spiral pattern whereas here we put the values in spiral order.
If you are preparing for a coding interview test, check out this list of questions.
Description
Let’s look at the problem details.
We are given an integer n and we want to fill a matrix of order n x n in spiral order.
example 1:
Input: n = 3Output:
1 2 3
8 9 4
7 6 5Explanation: The output is a matrix where elements are inserted in the spiral order.Example 2:
Input: n = 2Output:
1 2
4 3
Solution
The approach here is very very similar to what we did in the first part of the problem Spiral Matrix, there we were printing the values and here we have to add a value. 80% of the code can be copied from there.