Hardware interview practice
Traverse a matrix in spiral order
Return the elements of a rectangular matrix in clockwise spiral order.
Reviewed example
Work through one case
Input
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]Expected output
[1, 2, 3, 6, 9, 8, 7, 4, 5]Each perimeter is emitted clockwise before the bounds contract to the next inner layer.
What to cover
Requirements
- Support rectangular and single-row or single-column inputs.
- Assume every input row has the same number of columns.
- Guard the bottom and left traversals after shrinking the boundaries.
- Return an empty queue for an empty input.
