This problem really tests our knowledge on implementation - specifically simulation. So, since we need a spiral behaviour in the result, we can maintain 4 pointers - top, bottom, left, right. Think of these as bounds. Whenever while iterating, once we completed a row or a column, we can “shrink” our boundary on the respective side.
Code
Python3
Big O Analysis
Runtime
The runtime complexity here is O(N2) since we would be visiting all elements in the 2D matrix.
Memory
The memory usage is O(N) since we return the result in a list.