Member-only story

Spiral 🌀 Matrix II | Daily LeetCode Challenge | Day 13 | Coding Interview

Ganesh Prasad
3 min readApr 13, 2022

--

Spiral Matrix 2 | LeetCode challenge day 13 | Leetcode 59

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 = 3
Output:
1 2 3
8 9 4
7 6 5
Explanation: The output is a matrix where elements are inserted in the spiral order.Example 2:
Input: n = 2
Output:
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.

--

--

Ganesh Prasad
Ganesh Prasad

Written by Ganesh Prasad

Backend Developer at Appscrip | C++ veteran, 💜 Dart

No responses yet