Spiral Matrix Path Problem

You are provided with a two-dimensional array named MATRIX of size N x M, consisting of integers. Your task is to return the elements of the matrix following a spiral order.

Input:

The first line of input includes an integer 'T', representing the number of test cases or queries. Each test case starts with a line containing two integers 'N' and 'M', indicating the number of rows and columns, respectively. The following 'N' lines contain 'M' space-separated integers each, representing the rows of the matrix.

Output:

For each test case, output the elements of the matrix in a spiral order, with each test case output on a new line.

Example:

Consider a matrix:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

The spiral path would be:

1 2 3 6 9 8 7 4 5

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 10 ^ 2
  • 1 <= M <= 10 ^ 2
  • -10 ^ 9 <= MATRIX[i][j] <= 10 ^ 9

Note: You are not required to print anything on your own. Simply implement the function to achieve the correct output.

AnswerBot
4mo

Implement a function to return elements of a matrix in spiral order.

  • Iterate through the matrix in a spiral order by adjusting boundaries as you move along.

  • Keep track of the direction of movement (righ...read more

Anonymous
1y

def spiral_matrix(matrix):

result = []

while matrix:

result += matrix[0]

matrix = list(zip(*matrix[1:]))[::-1]

return result

# Example usage

MATRIX = [

[1, 2, 3],

[4, 5, 6],

[7, 8, 9]

]

spiral_path = sp...read more

Help your peers!
Select
Add answer anonymously...

Societe Generale Global Solution Centre Software Engineer interview questions & answers

A Software Engineer was asked 1mo agoQ. What are the main concepts of Object-Oriented Programming (OOP) in Java?
A Software Engineer was asked 3mo agoQ. Write code demonstrating exception handling.
A Software Engineer was asked 3mo agoQ. Write code and execute it to explain method overloading and overriding.

Popular interview questions of Software Engineer

A Software Engineer was asked 1mo agoQ1. What are the main concepts of Object-Oriented Programming (OOP) in Java?
A Software Engineer was asked 3mo agoQ2. Write code demonstrating exception handling.
A Software Engineer was asked 3mo agoQ3. Write code and execute it to explain method overloading and overriding.
Societe Generale Global Solution Centre Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits