DescriptionQ440
Q440FWASIC interview problem
Traverse a matrix in spiral order
TechniquesMatrixBoundary simulation
DifficultyMedium
TopicMatrices
LanguageSystemVerilog
Requirements4 checkpoints
01
Problem
Return the elements of a rectangular matrix in clockwise spiral order.
Example input and output
Use this case to check your interpretationInput
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]Output
[1, 2, 3, 6, 9, 8, 7, 4, 5]Explanation
Each perimeter is emitted clockwise before the bounds contract to the next inner layer.
02
Requirements (4)
- 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.
