Most solutions to algorithmic problems can be grouped into a rather small number of patterns. When we start to solve some problem, we need to think about how we would classify them. For example, can we apply fast and slowalgorithmic pattern or do we need to use cyclic sortpattern? Some of the problems have several solutions with different patterns. In this article of series Algorithms in Go we consider an algorithmic pattern that solves an entire class of the problems related to a matrix. Let's take one of such problems and see how we can handle it. How can we traverse a matrix in a spiral order? We can start with the observation that we are simulating a clock-wise movement and we continue the movement until we exhaust the matrix. How many movements will we have in total? We need to traverse all matrix, therefore the total number of moves will be equal to the total numbers of cells. Ok, then we have a loop condition: What do we do inside the loop? We start with the left movement: We proceed to the left till we reach the right border of the array, and then change the direction to the downward movement. We go down until we reach the bottom border and then start to move to the right. We reach the left border of the array and change the direction the last time and now move upwards. Let's move :) : How do we apply to move? We have four possible moves: If we didn't reach the border we just continue the movement in the current direction. Otherwise, we need to change the move. When we select the next cell, i.e next row and col values, we check the borders and change the direction if necessary: OK, now we can process the first layer of the matrix. How can we generalise the algorithm and process the whole matrix? We need to stop at the cell with value 1, as we already consumed it, and start the new circle. How do we start the new circle? We change the direction and instead of moving upwards 5 → 1 we go to the left 5 → 6. Therefore, we have a circle in a list of movements and we can help ourselves with a modulo operator: We also need to save all iterated cells and change the direction if we already processed the cell. Full listing: What complexity do we have? We touch every cell only once, so the time complexity is O(n*m). We have an auxiliary matrix seen, therefore our space complexity is also O(n*m). Can we do better than that? We cannot improve the time complexity as we need to visit all the cells in any case. However, we can think of removing the auxiliary matrix seen.  As discussed above we have four movements: left, down, right, up. We need to find a way to limit the range of the movements so we don't slip outside of the borders of the matrix and we don't process the same cell twice. Let's initialize the sentinels. We start with the left movement and iterate through all cells in the top row. After the iteration we increment top border, as the top row was fully consumed: Then we go downwards and iterate through all rows from top (which is now equal to one) to bottom inclusive. In this movement we fully consume the right column, therefore we decrease the right border: In the right movement, we consume all cells in the bottom row starting with the right column (which is now equal to last but one column) to the left column inclusive. After the iteration, we have fully consumed the bottom row and decrease the bottom border. Note that we go in the reversed direction, therefore we decrement the value of the current column after the iteration. Now, we close the circle and move upwards. We iterate through all the rows starting from the bottom (which is now equal to the last but one column) to the top row (which is now equal to the second row) inclusive. As in the previous movement, we go in the reverse direction and decrease the value of the current row. After the movement, we have fully consumed the left column and can increase the left counter. In this manner, we consume the first layer of the matrix. We continue the iteration till we exhaust the matrix. We need to stop when there are no more columns or no more rows left to process. When left becomes greater than right,  we have processed all the columns. When top becomes greater than bottom we have processed all the rows. In either of these two cases, we have no more cells to process. Therefore, our exit condition looks like the following: Let's check some edge cases to verify that our algorithm works as intended. What happens if we deal with a matrix of a single row? Our left move consumes all the cells, our downward move will be no-op as top will be already larger than the bottom. However, our right movement causes the problem as it re-consumes the cells from the last row which is the same as the first row. In the same vein, in case of a matrix of a single column, our upward movement becomes redundant. Therefore, we need to prevent such movements by the if condition: Therefore, the full listing looks as the following: Now we have time and space complexity both equal to O(n*m). We can use the same approach to the similar problems of matrix generation or matrix traversal. In this post, we implemented a solution for the Matrix Spiral problem. This is one of the most popular algorithmic patterns that solve an entire class of problems. More algorithmic patterns such as Sliding Window or Iterative Postorder Traversal can be found in the series Algorithms in Go.