Evaluate whether or not the larger array can be the result of merging two smaller arrays, maintaining order
$begingroup$
I'm prepping for a tech interview coming up and trying to have a better understanding of what I should look out for in writing code. Also, feel free to correct the terminology that I use as I want to be able to talk about my code clearly and succinctly.
One of the specifications for the tech interview process is that I use idiomatic JavaScript, according to the recruiter. I'm not familiar with that term and would appreciate any feedback on how to best write to that standard. Is there a specific standard that I should adhere to? Is that related to proper naming conventions?
Is there a better way for me to optimize time and space in my code?
When I assign values to variables within loops, are there rules that I should be aware of for when I shouldn't? For example, the current element being iterated over assigned to currentH1
variable, in my mind, makes it more readable in understanding what's happening as opposed to half1[h1Index]
, and I reuse it more than once. Plus, I believe this might make it more idiomatic but I'm not sure? Am I losing out on space complexity in any way? Or is there something that I may not be aware of by checking current
against undefined
instead of checking the current index against the size of the array?
When I assign values to variables outside of loops but within the function, are there space complexities that I should pay attention to or does garbage collection take care of this? Is this $O(1)$ space complexity as I'm just keeping track of the indices? Feel free to breakdown space complexity as I truly do want to have a more solid understanding of memory management.
I believe I've accounted for the edge cases that I can think of, but are there more that I should be aware of?
I placed the if condition for checking lengths at the top even before the index definitions because I figured that if they aren't even the same size, why bother with doing anything else. Is that weird?
function isMergedArray(half1, half2, mergedArray) {
if ((half1.length + half2.length) != mergedArray.length ) {
return false;
}
let h1Index = half1.length - 1;
let h2Index = half2.length - 1;
let maIndex = mergedArray.length - 1;
while (maIndex >= 0) {
const currentH1 = half1[h1Index];
const currentH2 = half2[h2Index];
const currentMa = mergedArray[maIndex];
if (currentH1 != undefined && currentH1 === currentMa) {
h1Index--;
} else if (currentH2 != undefined && currentH2 === currentMa) {
h2Index--;
} else {
return false;
}
maIndex--;
}
return true;
}
javascript performance algorithm memory-optimization
New contributor
$endgroup$
add a comment |
$begingroup$
I'm prepping for a tech interview coming up and trying to have a better understanding of what I should look out for in writing code. Also, feel free to correct the terminology that I use as I want to be able to talk about my code clearly and succinctly.
One of the specifications for the tech interview process is that I use idiomatic JavaScript, according to the recruiter. I'm not familiar with that term and would appreciate any feedback on how to best write to that standard. Is there a specific standard that I should adhere to? Is that related to proper naming conventions?
Is there a better way for me to optimize time and space in my code?
When I assign values to variables within loops, are there rules that I should be aware of for when I shouldn't? For example, the current element being iterated over assigned to currentH1
variable, in my mind, makes it more readable in understanding what's happening as opposed to half1[h1Index]
, and I reuse it more than once. Plus, I believe this might make it more idiomatic but I'm not sure? Am I losing out on space complexity in any way? Or is there something that I may not be aware of by checking current
against undefined
instead of checking the current index against the size of the array?
When I assign values to variables outside of loops but within the function, are there space complexities that I should pay attention to or does garbage collection take care of this? Is this $O(1)$ space complexity as I'm just keeping track of the indices? Feel free to breakdown space complexity as I truly do want to have a more solid understanding of memory management.
I believe I've accounted for the edge cases that I can think of, but are there more that I should be aware of?
I placed the if condition for checking lengths at the top even before the index definitions because I figured that if they aren't even the same size, why bother with doing anything else. Is that weird?
function isMergedArray(half1, half2, mergedArray) {
if ((half1.length + half2.length) != mergedArray.length ) {
return false;
}
let h1Index = half1.length - 1;
let h2Index = half2.length - 1;
let maIndex = mergedArray.length - 1;
while (maIndex >= 0) {
const currentH1 = half1[h1Index];
const currentH2 = half2[h2Index];
const currentMa = mergedArray[maIndex];
if (currentH1 != undefined && currentH1 === currentMa) {
h1Index--;
} else if (currentH2 != undefined && currentH2 === currentMa) {
h2Index--;
} else {
return false;
}
maIndex--;
}
return true;
}
javascript performance algorithm memory-optimization
New contributor
$endgroup$
$begingroup$
(Welcome to Code Review!) 2¢: comment your code; automate tests.
$endgroup$
– greybeard
14 mins ago
add a comment |
$begingroup$
I'm prepping for a tech interview coming up and trying to have a better understanding of what I should look out for in writing code. Also, feel free to correct the terminology that I use as I want to be able to talk about my code clearly and succinctly.
One of the specifications for the tech interview process is that I use idiomatic JavaScript, according to the recruiter. I'm not familiar with that term and would appreciate any feedback on how to best write to that standard. Is there a specific standard that I should adhere to? Is that related to proper naming conventions?
Is there a better way for me to optimize time and space in my code?
When I assign values to variables within loops, are there rules that I should be aware of for when I shouldn't? For example, the current element being iterated over assigned to currentH1
variable, in my mind, makes it more readable in understanding what's happening as opposed to half1[h1Index]
, and I reuse it more than once. Plus, I believe this might make it more idiomatic but I'm not sure? Am I losing out on space complexity in any way? Or is there something that I may not be aware of by checking current
against undefined
instead of checking the current index against the size of the array?
When I assign values to variables outside of loops but within the function, are there space complexities that I should pay attention to or does garbage collection take care of this? Is this $O(1)$ space complexity as I'm just keeping track of the indices? Feel free to breakdown space complexity as I truly do want to have a more solid understanding of memory management.
I believe I've accounted for the edge cases that I can think of, but are there more that I should be aware of?
I placed the if condition for checking lengths at the top even before the index definitions because I figured that if they aren't even the same size, why bother with doing anything else. Is that weird?
function isMergedArray(half1, half2, mergedArray) {
if ((half1.length + half2.length) != mergedArray.length ) {
return false;
}
let h1Index = half1.length - 1;
let h2Index = half2.length - 1;
let maIndex = mergedArray.length - 1;
while (maIndex >= 0) {
const currentH1 = half1[h1Index];
const currentH2 = half2[h2Index];
const currentMa = mergedArray[maIndex];
if (currentH1 != undefined && currentH1 === currentMa) {
h1Index--;
} else if (currentH2 != undefined && currentH2 === currentMa) {
h2Index--;
} else {
return false;
}
maIndex--;
}
return true;
}
javascript performance algorithm memory-optimization
New contributor
$endgroup$
I'm prepping for a tech interview coming up and trying to have a better understanding of what I should look out for in writing code. Also, feel free to correct the terminology that I use as I want to be able to talk about my code clearly and succinctly.
One of the specifications for the tech interview process is that I use idiomatic JavaScript, according to the recruiter. I'm not familiar with that term and would appreciate any feedback on how to best write to that standard. Is there a specific standard that I should adhere to? Is that related to proper naming conventions?
Is there a better way for me to optimize time and space in my code?
When I assign values to variables within loops, are there rules that I should be aware of for when I shouldn't? For example, the current element being iterated over assigned to currentH1
variable, in my mind, makes it more readable in understanding what's happening as opposed to half1[h1Index]
, and I reuse it more than once. Plus, I believe this might make it more idiomatic but I'm not sure? Am I losing out on space complexity in any way? Or is there something that I may not be aware of by checking current
against undefined
instead of checking the current index against the size of the array?
When I assign values to variables outside of loops but within the function, are there space complexities that I should pay attention to or does garbage collection take care of this? Is this $O(1)$ space complexity as I'm just keeping track of the indices? Feel free to breakdown space complexity as I truly do want to have a more solid understanding of memory management.
I believe I've accounted for the edge cases that I can think of, but are there more that I should be aware of?
I placed the if condition for checking lengths at the top even before the index definitions because I figured that if they aren't even the same size, why bother with doing anything else. Is that weird?
function isMergedArray(half1, half2, mergedArray) {
if ((half1.length + half2.length) != mergedArray.length ) {
return false;
}
let h1Index = half1.length - 1;
let h2Index = half2.length - 1;
let maIndex = mergedArray.length - 1;
while (maIndex >= 0) {
const currentH1 = half1[h1Index];
const currentH2 = half2[h2Index];
const currentMa = mergedArray[maIndex];
if (currentH1 != undefined && currentH1 === currentMa) {
h1Index--;
} else if (currentH2 != undefined && currentH2 === currentMa) {
h2Index--;
} else {
return false;
}
maIndex--;
}
return true;
}
javascript performance algorithm memory-optimization
javascript performance algorithm memory-optimization
New contributor
New contributor
edited 1 hour ago
Jamal♦
30.3k11116226
30.3k11116226
New contributor
asked 4 hours ago
user4860user4860
11
11
New contributor
New contributor
$begingroup$
(Welcome to Code Review!) 2¢: comment your code; automate tests.
$endgroup$
– greybeard
14 mins ago
add a comment |
$begingroup$
(Welcome to Code Review!) 2¢: comment your code; automate tests.
$endgroup$
– greybeard
14 mins ago
$begingroup$
(Welcome to Code Review!) 2¢: comment your code; automate tests.
$endgroup$
– greybeard
14 mins ago
$begingroup$
(Welcome to Code Review!) 2¢: comment your code; automate tests.
$endgroup$
– greybeard
14 mins ago
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
});
}
});
user4860 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f212112%2fevaluate-whether-or-not-the-larger-array-can-be-the-result-of-merging-two-smalle%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
user4860 is a new contributor. Be nice, and check out our Code of Conduct.
user4860 is a new contributor. Be nice, and check out our Code of Conduct.
user4860 is a new contributor. Be nice, and check out our Code of Conduct.
user4860 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f212112%2fevaluate-whether-or-not-the-larger-array-can-be-the-result-of-merging-two-smalle%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
$begingroup$
(Welcome to Code Review!) 2¢: comment your code; automate tests.
$endgroup$
– greybeard
14 mins ago