Skip to the solution
MediumFrontend6 min read

What do flat and flatMap do?

692 views

The problem

Question presented to candidate: "If you call array.flat() with no arguments on a deeply nested array, how deep does it actually flatten? And is array.flatMap(fn) exactly the same as array.map(fn).flat(), or is there a real difference?"

What a strong answer should cover:

  • πŸ“Œ Interview term: flat(depth) β€” returns a new array with sub-array elements concatenated up to the specified depth β€” the default depth is exactly 1, verified directly: a triple-nested array called with no arguments genuinely only flattens ONE level, leaving a deeper sub-array intact.
  • πŸ“Œ Interview term: flat(Infinity) β€” verified directly: passing Infinity as the depth genuinely flattens arbitrarily deep nesting completely, regardless of how many levels exist.
  • πŸ“Œ Interview term: the real, direct answer to the prompt's flatMap question β€” verified directly: arr.flatMap(fn) and arr.map(fn).flat() genuinely produce the identical result for the same input β€” the real, practical difference is that flatMap does it in a single pass, slightly more efficient than creating an intermediate mapped array first.
  • πŸ“Œ Interview term: flatMap only flattens ONE level, always β€” verified directly: even when the mapped callback's return value is nested TWO levels deep, flatMap genuinely only flattens the first level, leaving the deeper nesting intact β€” unlike flat(), its depth is not configurable at all.
  • A precise answer names that flat() genuinely removes sparse array holes as a side effect of flattening β€” verified directly β€” a real, secondary behavior beyond just concatenating nested arrays.

Clarifying questions expected:

  • None β€” this is a definitional/comparison question; directly answering the prompt's default-depth and flatMap-equivalence questions with real proof is the strong signal.

Code / implementation expected: Yes β€” the direct depth comparisons (default, explicit depth, Infinity) plus the flatMap-vs-map-then-flat side-by-side are the clearest, most convincing demonstrations.

  • flat
  • flatmap

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 proof: flat()'s default depth is exactly 1, flatMap equals map().flat() but in one pass, and flatMap always flattens exactly one level

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 68 of 165 in JavaScript

Keep your momentum going.

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

Up nextMedium

How does the 'this' keyword work?

Continue