Good morning! Here's our prompt for today.
Given an m * n
matrix array, can you print all its elements in a spiral order as shown in the figure below? Try to use only O(1)
space!

Constraints
- Total elements in the matrix <=
100000
- The values in the matrix ranges from
-1000000000
and1000000000
- Expected time complexity :
O(n*m)
wheren
andm
are the rows and columns respectively - Expected space complexity :
O(1)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
58
'PASSED: `spiraltraverse([[1,2,3,4], [5,6,7,8 ], [9,10,11,12], [13,14,15,16]])` should return `1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10`'
var assert = require('assert');
​
function spiraltraverse(inmatrix) {
// fill this in
}
​
try {
assert.equal(
spiraltraverse([
[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12],
]),
'1,2,3,4,5,6,12,11,10,9,8,7'
);
​
console.log(
'PASSED: `spiraltraverse([[1,2,3,4,5,6], [7,8,9,10,11,12]])` should return `1,2,3,4,5,6,12,11,10,9,8,7 `'
);
} catch (err) {
console.log(err);
}
​
try {
assert.equal(
spiraltraverse([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Here's how we would solve this problem...
How do I use this guide?