JavaScript Spiral Matrix Coding Challenge
$begingroup$
A whiteboarding challenge: Given a 2D array (matrix) inputMatrix of integers, create a function spiralCopy that copies inputMatrix’s values into a 1D array in a clockwise spiral order. Your function then should return that array.
inputMatrix = [
[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30],
[31, 32, 33, 34, 35, 36]
]
const spiralCopy = (matrix) => {
const spiralLength = matrix.length * matrix[0].length;
const spiral = ;
let iLeftRightStart = 0; // +1
let iLeftRightEnd = matrix[0].length; // -1
let leftRightPosition = 0; // +1
let iTopBottomStart = 1; // +1
let iTopBottomEnd = matrix.length; // -1
let topBottomPosition = matrix[0].length - 1; // -1
let iRightLeftStart = matrix[0].length - 2; // -1
let iRightLeftEnd = 0; // +1
let rightLeftPosition = matrix.length - 1; // -1
let iBottomTopStart = matrix.length - 2; // -1
let iBottomTopEnd = 1; // +1
let bottomTopPosition = 0; // +1
const leftToRight = (iStart, iEnd, position) => {
for (let i = iStart; i < iEnd; i++) {
spiral.push(matrix[position][i]);
}
}
const topToBottom = (iStart, iEnd, position) => {
for (let i = iStart; i < iEnd; i++) {
spiral.push(matrix[i][position]);
}
}
const rightToLeft = (iStart, iEnd, position) => {
for (let i = iStart; i >= iEnd; i--) {
spiral.push(matrix[position][i]);
}
}
const bottomToTop = (iStart, iEnd, position) => {
for (let i = iStart; i >= iEnd; i--) {
spiral.push(matrix[i][position]);
}
}
while (spiral.length < spiralLength) {
leftToRight(iLeftRightStart, iLeftRightEnd, leftRightPosition);
topToBottom(iTopBottomStart, iTopBottomEnd, topBottomPosition);
rightToLeft(iRightLeftStart, iRightLeftEnd, rightLeftPosition);
bottomToTop(iBottomTopStart, iBottomTopEnd, bottomTopPosition);
iLeftRightStart++;
iLeftRightEnd--;
leftRightPosition++;
iTopBottomStart++;
iTopBottomEnd--;
topBottomPosition--;
iRightLeftStart--;
iRightLeftEnd++;
rightLeftPosition--;
iBottomTopStart--;
iBottomTopEnd++;
bottomTopPosition++;
}
return spiral;
}
console.log(spiralCopy(inputMatrix));
// prints [1, 2, 3, 4, 5, 6, 12, 18, 24, 30, 36, 35, 34, 33, 32, 31, 25, 19, 13, 7, 8, 9, 10, 11, 17, 23, 29, 28, 27, 26, 20, 14, 15, 16, 22, 21]
So the function works and runs with time complexity of O(N). However, I don't like that I'm using 12 different variables which I then increment/decrement. How can this be consolidated, and is it possible to make the algorithm even faster due to the instant access of JavaScript arrays?
javascript programming-challenge array matrix
$endgroup$
add a comment |
$begingroup$
A whiteboarding challenge: Given a 2D array (matrix) inputMatrix of integers, create a function spiralCopy that copies inputMatrix’s values into a 1D array in a clockwise spiral order. Your function then should return that array.
inputMatrix = [
[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30],
[31, 32, 33, 34, 35, 36]
]
const spiralCopy = (matrix) => {
const spiralLength = matrix.length * matrix[0].length;
const spiral = ;
let iLeftRightStart = 0; // +1
let iLeftRightEnd = matrix[0].length; // -1
let leftRightPosition = 0; // +1
let iTopBottomStart = 1; // +1
let iTopBottomEnd = matrix.length; // -1
let topBottomPosition = matrix[0].length - 1; // -1
let iRightLeftStart = matrix[0].length - 2; // -1
let iRightLeftEnd = 0; // +1
let rightLeftPosition = matrix.length - 1; // -1
let iBottomTopStart = matrix.length - 2; // -1
let iBottomTopEnd = 1; // +1
let bottomTopPosition = 0; // +1
const leftToRight = (iStart, iEnd, position) => {
for (let i = iStart; i < iEnd; i++) {
spiral.push(matrix[position][i]);
}
}
const topToBottom = (iStart, iEnd, position) => {
for (let i = iStart; i < iEnd; i++) {
spiral.push(matrix[i][position]);
}
}
const rightToLeft = (iStart, iEnd, position) => {
for (let i = iStart; i >= iEnd; i--) {
spiral.push(matrix[position][i]);
}
}
const bottomToTop = (iStart, iEnd, position) => {
for (let i = iStart; i >= iEnd; i--) {
spiral.push(matrix[i][position]);
}
}
while (spiral.length < spiralLength) {
leftToRight(iLeftRightStart, iLeftRightEnd, leftRightPosition);
topToBottom(iTopBottomStart, iTopBottomEnd, topBottomPosition);
rightToLeft(iRightLeftStart, iRightLeftEnd, rightLeftPosition);
bottomToTop(iBottomTopStart, iBottomTopEnd, bottomTopPosition);
iLeftRightStart++;
iLeftRightEnd--;
leftRightPosition++;
iTopBottomStart++;
iTopBottomEnd--;
topBottomPosition--;
iRightLeftStart--;
iRightLeftEnd++;
rightLeftPosition--;
iBottomTopStart--;
iBottomTopEnd++;
bottomTopPosition++;
}
return spiral;
}
console.log(spiralCopy(inputMatrix));
// prints [1, 2, 3, 4, 5, 6, 12, 18, 24, 30, 36, 35, 34, 33, 32, 31, 25, 19, 13, 7, 8, 9, 10, 11, 17, 23, 29, 28, 27, 26, 20, 14, 15, 16, 22, 21]
So the function works and runs with time complexity of O(N). However, I don't like that I'm using 12 different variables which I then increment/decrement. How can this be consolidated, and is it possible to make the algorithm even faster due to the instant access of JavaScript arrays?
javascript programming-challenge array matrix
$endgroup$
add a comment |
$begingroup$
A whiteboarding challenge: Given a 2D array (matrix) inputMatrix of integers, create a function spiralCopy that copies inputMatrix’s values into a 1D array in a clockwise spiral order. Your function then should return that array.
inputMatrix = [
[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30],
[31, 32, 33, 34, 35, 36]
]
const spiralCopy = (matrix) => {
const spiralLength = matrix.length * matrix[0].length;
const spiral = ;
let iLeftRightStart = 0; // +1
let iLeftRightEnd = matrix[0].length; // -1
let leftRightPosition = 0; // +1
let iTopBottomStart = 1; // +1
let iTopBottomEnd = matrix.length; // -1
let topBottomPosition = matrix[0].length - 1; // -1
let iRightLeftStart = matrix[0].length - 2; // -1
let iRightLeftEnd = 0; // +1
let rightLeftPosition = matrix.length - 1; // -1
let iBottomTopStart = matrix.length - 2; // -1
let iBottomTopEnd = 1; // +1
let bottomTopPosition = 0; // +1
const leftToRight = (iStart, iEnd, position) => {
for (let i = iStart; i < iEnd; i++) {
spiral.push(matrix[position][i]);
}
}
const topToBottom = (iStart, iEnd, position) => {
for (let i = iStart; i < iEnd; i++) {
spiral.push(matrix[i][position]);
}
}
const rightToLeft = (iStart, iEnd, position) => {
for (let i = iStart; i >= iEnd; i--) {
spiral.push(matrix[position][i]);
}
}
const bottomToTop = (iStart, iEnd, position) => {
for (let i = iStart; i >= iEnd; i--) {
spiral.push(matrix[i][position]);
}
}
while (spiral.length < spiralLength) {
leftToRight(iLeftRightStart, iLeftRightEnd, leftRightPosition);
topToBottom(iTopBottomStart, iTopBottomEnd, topBottomPosition);
rightToLeft(iRightLeftStart, iRightLeftEnd, rightLeftPosition);
bottomToTop(iBottomTopStart, iBottomTopEnd, bottomTopPosition);
iLeftRightStart++;
iLeftRightEnd--;
leftRightPosition++;
iTopBottomStart++;
iTopBottomEnd--;
topBottomPosition--;
iRightLeftStart--;
iRightLeftEnd++;
rightLeftPosition--;
iBottomTopStart--;
iBottomTopEnd++;
bottomTopPosition++;
}
return spiral;
}
console.log(spiralCopy(inputMatrix));
// prints [1, 2, 3, 4, 5, 6, 12, 18, 24, 30, 36, 35, 34, 33, 32, 31, 25, 19, 13, 7, 8, 9, 10, 11, 17, 23, 29, 28, 27, 26, 20, 14, 15, 16, 22, 21]
So the function works and runs with time complexity of O(N). However, I don't like that I'm using 12 different variables which I then increment/decrement. How can this be consolidated, and is it possible to make the algorithm even faster due to the instant access of JavaScript arrays?
javascript programming-challenge array matrix
$endgroup$
A whiteboarding challenge: Given a 2D array (matrix) inputMatrix of integers, create a function spiralCopy that copies inputMatrix’s values into a 1D array in a clockwise spiral order. Your function then should return that array.
inputMatrix = [
[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30],
[31, 32, 33, 34, 35, 36]
]
const spiralCopy = (matrix) => {
const spiralLength = matrix.length * matrix[0].length;
const spiral = ;
let iLeftRightStart = 0; // +1
let iLeftRightEnd = matrix[0].length; // -1
let leftRightPosition = 0; // +1
let iTopBottomStart = 1; // +1
let iTopBottomEnd = matrix.length; // -1
let topBottomPosition = matrix[0].length - 1; // -1
let iRightLeftStart = matrix[0].length - 2; // -1
let iRightLeftEnd = 0; // +1
let rightLeftPosition = matrix.length - 1; // -1
let iBottomTopStart = matrix.length - 2; // -1
let iBottomTopEnd = 1; // +1
let bottomTopPosition = 0; // +1
const leftToRight = (iStart, iEnd, position) => {
for (let i = iStart; i < iEnd; i++) {
spiral.push(matrix[position][i]);
}
}
const topToBottom = (iStart, iEnd, position) => {
for (let i = iStart; i < iEnd; i++) {
spiral.push(matrix[i][position]);
}
}
const rightToLeft = (iStart, iEnd, position) => {
for (let i = iStart; i >= iEnd; i--) {
spiral.push(matrix[position][i]);
}
}
const bottomToTop = (iStart, iEnd, position) => {
for (let i = iStart; i >= iEnd; i--) {
spiral.push(matrix[i][position]);
}
}
while (spiral.length < spiralLength) {
leftToRight(iLeftRightStart, iLeftRightEnd, leftRightPosition);
topToBottom(iTopBottomStart, iTopBottomEnd, topBottomPosition);
rightToLeft(iRightLeftStart, iRightLeftEnd, rightLeftPosition);
bottomToTop(iBottomTopStart, iBottomTopEnd, bottomTopPosition);
iLeftRightStart++;
iLeftRightEnd--;
leftRightPosition++;
iTopBottomStart++;
iTopBottomEnd--;
topBottomPosition--;
iRightLeftStart--;
iRightLeftEnd++;
rightLeftPosition--;
iBottomTopStart--;
iBottomTopEnd++;
bottomTopPosition++;
}
return spiral;
}
console.log(spiralCopy(inputMatrix));
// prints [1, 2, 3, 4, 5, 6, 12, 18, 24, 30, 36, 35, 34, 33, 32, 31, 25, 19, 13, 7, 8, 9, 10, 11, 17, 23, 29, 28, 27, 26, 20, 14, 15, 16, 22, 21]
So the function works and runs with time complexity of O(N). However, I don't like that I'm using 12 different variables which I then increment/decrement. How can this be consolidated, and is it possible to make the algorithm even faster due to the instant access of JavaScript arrays?
javascript programming-challenge array matrix
javascript programming-challenge array matrix
asked 58 mins ago
Sean ValdiviaSean Valdivia
3751414
3751414
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215214%2fjavascript-spiral-matrix-coding-challenge%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215214%2fjavascript-spiral-matrix-coding-challenge%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown