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.
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!
Add answer anonymously...
Top Societe Generale Global Solution Centre Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Top HR questions asked in Societe Generale Global Solution Centre Software Engineer
>
Societe Generale Global Solution Centre Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app