Skip to the solution
EasyDSAAsked in 20246 min read

Zip Arrays

2.7k views

The problem

Problem, as an interviewer would state it: "Implement zip(...arrays) — combining multiple arrays into a single array of grouped tuples, where the Nth tuple contains the Nth element from every input array, matching lodash's own well-known _.zip behavior."

Examples:

Code
zip([1,2,3], ["a","b","c"]); // [[1,"a"], [2,"b"], [3,"c"]]

Clarifying questions expected:

  • What should the real, correct output length be if the input arrays have DIFFERENT lengths?
  • For a shorter array, what value should genuinely appear in the tuple slot beyond its own length?
  • Does this need to support any number of input arrays, or just exactly two?

Code / implementation expected: Yes — real, direct proof zipping equal-length arrays, zipping unequal-length arrays (confirming the missing slot is genuinely undefined), and confirming any number of input arrays is supported.

  • array

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: zip() correctly combines equal and unequal-length arrays, with a direct check confirming a missing slot is genuinely undefined

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 6 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

LocalStorage wrapper items TTL

Continue