Skip to the solution
MediumDSAAsked in 20246 min read

Implement pick(obj, paths) Supporting Both Shallow and Nested Paths

3.9k views

The problem

Problem, as an interviewer would state it: "Implement pick(obj, paths) — building a genuinely NEW object containing only the specified paths (both shallow keys and nested dot-paths), matching lodash's own well-known _.pick behavior extended to support nested paths."

Examples:

Code
pick({a:1, b:2, c:{d:3, e:4}}, ["a", "c.d"]); // {a:1, c:{d:3}}

Clarifying questions expected:

  • Does this need to support NESTED dot-paths (like "c.d"), not just shallow top-level keys?
  • If a specified path is genuinely missing from the source object, should it be silently skipped, or should an error be raised?
  • Should the original object remain completely untouched?

Code / implementation expected: Yes — real, direct proof of both shallow and nested path selection, confirmation a genuinely missing path is silently skipped, and confirmation the original object is never mutated.

  • object
  • recursion

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: pick() built on this bank's own get/set primitives correctly selects both shallow and nested paths, silently skipping a genuinely missing one

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

Priority queue binary heap

Continue