Read a matrix in a spiral
Return the values of a rectangular matrix in clockwise spiral order starting at the top-left. Empty input returns [].
matrix = [[1, 2, 3], [4, 5, 6]]
top, left, right = 0, 0, 2
print(matrix[top][left:right+1])Reason about the state
Maintain four exclusive or inclusive boundaries and shrink after each edge. Recheck the remaining height and width before reading the opposite edges. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the values of a rectangular matrix in clockwise spiral order starting at the top-left. Empty input returns [].
The explanation opens here once every check passes.