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 the boundaries of rows and columns.

  • Keep track of the direction of traversa...read more

Help your peers!
Select
Add answer anonymously...

Adobe Software Quality Engineer interview questions & answers

A Software Quality Engineer was asked Q. Leaders in an Array Problem Statement You are given a sequence of numbers. Your ...read more
A Software Quality Engineer was asked Q. Spiral Matrix Path Problem You are provided with a two-dimensional array named M...read more
A Software Quality Engineer was asked Q. Stack using Two Queues Problem Statement Develop a Stack Data Structure to store...read more

Popular interview questions of Software Quality Engineer

A Software Quality Engineer was asked Q1. Leaders in an Array Problem Statement You are given a sequence of numbers. Your ...read more
A Software Quality Engineer was asked Q2. Spiral Matrix Path Problem You are provided with a two-dimensional array named M...read more
A Software Quality Engineer was asked Q3. Stack using Two Queues Problem Statement Develop a Stack Data Structure to store...read more
Adobe Software Quality 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