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.
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
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
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
Reviews
Interviews
Salaries
Users/Month