Check whether an array can be the result of merging two smaller arrays, maintaining order 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 &gt;= 0) {
    const currentH1 = half1[h1Index];
    const currentH2 = half2[h2Index];
    const currentMa = mergedArray[maIndex];

    if (currentH1 != undefined &amp;&amp; currentH1 === currentMa) {
      h1Index--;
    } else if (currentH2 != undefined &amp;&amp; currentH2 === currentMa) {
      h2Index--;
    } else {
      return false;
    }

    maIndex--;
  }

  return true;
}
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?


No I don't think that is weird. I would consider it a good optimization.

Personally I think the code is mostly fine, a good starting point would be to Google some examples on how to merge sub-arrays since that is fairly similar.

My one question would be why you started at the end of the array and merged them in descending order. That is kind of strange to me. I would do it in ascending order. I would also use a for loop instead of a while loop.