Spiral Matrix Traversal
2D Arrays (Matrices) DSA practice problem on Onlearn.
Difficulty: medium.
Topics: Print a matrix in spiral order, Matrices, Two-dimensional Array Traversal, Loops, Conditional Statements, Time Complexity, Space Complexity, matrix, edge cases, space complexity, general programming, matrix traversal, time complexity analysis, Matrix Traversal, Loop Invariants, Edge Case Handling in Arrays.
Problem Statement Given an M x N matrix, return all elements of the matrix in spiral order. Input Specification The input consists of a 2D integer array (matrix). Output Specification Return a list of integers representing the elements of the matrix in spiral order. Sample Test Cases Example 1: Input: Matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ] Output: [1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10] Example 2: Input: Matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]