Spiral Traverse

A corgi smiling happily

Write a function that takes in an n x m two-dimensional array (that can be square-shaped when n == m) and returns a one-dimensional array of all the array’s elements in spiral order.

Spiral order starts in the top left corner of the two-dimensional array, goes to the right, and proceeds in a spiral pattern all the way until every element has been visited.

Sample Input

1array = [
2 [1, 2, 3, 4],
3 [12, 13, 14, 5],
4 [11, 16, 15, 6],
5 [10, 9, 8, 7],
6]

Sample Output

1[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

Hints

Hint 1

You can think of the spiral that you have to traverse as a set of rectangle perimeters that progressively get smaller (i.e., that progressively move inwards in the two-dimensional array).

Hint 2

Going off of Hint #1, declare four variables: a starting row, a starting column, an ending row, and an ending column. These four variables represent the bounds of the first rectangle perimeter in the spiral that you have to traverse. Traverse that perimeter using those bounds, and then move the bounds inwards. End your algorithm once the starting row passes the ending row or the starting column passes the ending column.

Hint 3

You can solve this problem both iteratively and recursively following very similar logic.

Optimal Space & Time Complexity

O(n) time | O(n) space - where n is the total number of elements in the array

Solution-1
1// O(n) time | O(n) space - where n is the total number of elements in the array
2function spiralTraverse(array) {
3 // Write your code here.
4
5 let arr = []
6 let startRow = 0, endRow = array.length - 1
7 let startCol = 0, endCol = array[0].length - 1
8
9 while (startRow <= endRow && startCol <= endCol) {
10 for (let col = startCol; col <= endCol; col++) {
11 arr.push(array[startRow][col])
12 }
13
14 for (let row = startRow + 1; row <= endRow; row++) {
15 arr.push(array[row][endCol])
16 }
17
18 for (let col = endCol - 1; col >= startCol; col--) {
19 if (startRow === endRow) break
20 arr.push(array[endRow][col])
21 }
22
23 for (let row = endRow - 1; row > startRow; row--) {
24 if (startCol === endCol) break
25 arr.push(array[row][startCol])
26 }
27
28 startRow++;
29 endRow--;
30 startCol++;
31 endCol--;
32 }
33
34 console.log(arr)
35 return arr
36}
Solution-2
1function spiralTraverse(array) {
2 const result = []
3 spiralFill(array, 0, array.length - 1, 0, array[0].length - 1, result)
4 return result
5}
6
7function spiralFill(array, startRow, endRow, startCol, endCol, result) {
8 if (startRow > endRow || startCol > endCol) return;
9
10 for (let col = startCol; col <= endCol; col++) {
11 result.push(array[startRow][col])
12 }
13
14 for (let row = startRow + 1; row <= endRow; row++) {
15 result.push(array[row][endCol])
16 }
17
18 for (let col = endCol - 1; col >= startCol; col--) {
19 if (startRow === endRow) break;
20 result.push(array[endRow][col])
21 }
22
23 for (let row = endRow - 1; row > startRow; row--) {
24 if (startCol === endCol) break
25 result.push(array[row][startCol])
26 }
27
28 spiralFill(array, startRow + 1, endRow - 1, startCol + 1, endCol - 1, result)
29
30}

🌤️

Do you have any questions, or simply wish to contact me privately? Don't hesitate to shoot me a DM on Twitter.

Have a wonderful day.
Abhishek 🙏

Subscribe to my newsletter

Get email from me about my ideas, full-stack development resources, tricks and tips as well as exclusive previews of upcoming articles.

No spam. Just the highest quality ideas you’ll find on the web.