Programming interview practice of the week (October 25)
Write a function that accepts four arguments. The first two arguments are the size of the grid (h x w), filled with ascending integers from left to right, top to bottom, starting from 1. The next two arguments are is the starting positions, the row (r) and column (c).
Return an array of integers obtained by spiraling outward anti-clockwise from the r and c, starting upward.
Note: There is a typo in the question. We should replace the number 194
with 14
in the second test case.
You can check the algorithm with:
$ pip install pytest
$ py.test
It executes in O(n)
where n
is the spiral size not the layout h * w
. It should be memory efficient (O(h * w)
) as it only stores the result representation.