FastPrepS-Shaped Matrix Traversal

S-Shaped Matrix Traversal

Fox Corporation logoFox Corporation● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Traverse a nonempty square matrix row by row in an S shape: read row 0 left to right, row 1 right to left, and continue alternating directions.

Return the visited values in order.

Function

sShapedTraversal(matrix: int[][]) → int[]

Examples

Example 1

matrix = [[1]]return = [1]

Case 1 exercises the documented deterministic contract.

Example 2

matrix = [[1,2],[3,4]]return = [1,2,4,3]

Case 2 exercises the documented deterministic contract.

Example 3

matrix = [[1,2,3],[4,5,6],[7,8,9]]return = [1,2,3,6,5,4,7,8,9]

Case 3 exercises the documented deterministic contract.

Constraints

  • 1 <= matrix.length == matrix[i].length <= 1000.
  • Matrix values are signed 32-bit integers.

More Fox Corporation problems

See Fox Corporation hiring insights
public int[] sShapedTraversal(int[][] matrix) {
    // Write your code here.
}
matrix[[1]]
expected[1]
Checking account…