Skip to the solution
MediumFrontend6 min read

When should you use Map and Set over objects and arrays?

1.1k views

The problem

Question presented to candidate: "If you use an object as a key on a plain JavaScript object, like obj[someObject] = value, what actually happens to that key? Would a Map handle it differently?"

What a strong answer should cover:

  • πŸ“Œ Interview term: object key coercion β€” verified directly: using an object as a key on a plain {} genuinely coerces it to the literal string "[object Object]" β€” every distinct object used as a key this way collides on the exact same string, a real, silent data-corruption bug.
  • πŸ“Œ Interview term: Map's real, direct answer to the prompt β€” verified directly: a Map genuinely accepts the object itself, with no coercion at all, as a real, distinct key β€” two different object references are genuinely two different, non-colliding keys.
  • πŸ“Œ Interview term: Map has a real, direct .size β€” verified directly: unlike a plain object (which requires Object.keys(obj).length to count entries), a Map genuinely has a real .size property, always accurate.
  • πŸ“Œ Interview term: Set dedupes while keeping type-DISTINCT values separate β€” verified directly: a Set built from [1, "1", 1, 2, 2] genuinely keeps both the number 1 and the string "1" as two SEPARATE entries (using SameValueZero comparison, the same algorithm covered in this bank's own includes vs. indexOf question), while still correctly removing the genuine duplicate 1s and 2s.
  • A precise answer names that Map/Set also genuinely guarantee real insertion-order iteration (a formal spec guarantee, not just an implementation detail) and are directly iterable via for...of β€” while a plain object/array can be used similarly in PRACTICE for simple cases, but without the same formal guarantees or capabilities (arbitrary key types, guaranteed size, no prototype-pollution risk from inherited properties).

Clarifying questions expected:

  • None β€” this is a definitional/comparison question; directly answering the prompt's object-key coercion scenario (and Map's real fix) is the strong signal.

Code / implementation expected: Yes β€” the side-by-side object-key-coercion vs. Map-object-key test is the clearest, most convincing demonstration.

  • map
  • set

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 Β· 8 sections

Run the code

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

JavaScriptReal proof: a plain object coerces an object key to '[object Object]' (a real collision bug) while Map genuinely accepts it as a distinct key

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 50 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

What is memoization?

Continue