Spiral Matrix Problem Statement

You are given a N x M matrix of integers. Your task is to return the spiral path of the matrix elements.

Input

The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains two single space separated integers N and M, denoting the number of rows and columns respectively.
The next 'N' lines, each containing 'M' single space-separated integers representing the elements in a row of the matrix.

Output

For each test case/query, print the spiral path of the given matrix.
Output for every test case will be printed in a separate line.

Example:

Example Of Spiral Path:

Spiral Path

Constraints:

  • 1 ≤ T ≤ 5
  • 1 ≤ N ≤ 102
  • 1 ≤ M ≤ 102
  • -109 ≤ mat[i][j] ≤ 109
  • Time Limit: 1 second
Note:

You are not required to print anything; it has already been taken care of. Just implement the given function.

AnswerBot
4mo

The task is to return the spiral path of elements in a given matrix.

  • Iterate through the matrix in a spiral path by adjusting the boundaries at each step.

  • Keep track of the direction of traversal (right...read more

Help your peers!
Select
Add answer anonymously...

Symphony Talent Software Developer interview questions & answers

A Software Developer was asked Q. Height of Binary Tree You are provided with the Inorder and Level Order traversa...read more
A Software Developer was asked Q. Matrix Symmetry Check Problem Statement You are provided with a square matrix. Y...read more
A Software Developer was asked Q. Cycle Detection in a Singly Linked List Determine if a given singly linked list ...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Height of Binary Tree You are provided with the Inorder and Level Order traversa...read more
A Software Developer was asked Q2. Matrix Symmetry Check Problem Statement You are provided with a square matrix. Y...read more
A Software Developer was asked Q3. Cycle Detection in a Singly Linked List Determine if a given singly linked list ...read more
Symphony Talent Software Developer 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