FrontendInterviews.dev

Loading problem…

276. Unflatten Object

Medium•

Convert a flat object with dot-separated paths into a nested object.

Requirements

Implement:

function unflattenObject(flatObj) {}

Behavior:

  • Dot notation creates nested objects ({"a.b": 1} => { a: { b: 1 } })
  • Plain keys stay at root
  • If a non-object blocks a path, replace it with an object to continue nesting
  • Return {} for invalid non-object input

Example Usage

unflattenObject({
  'user.name': 'Sam',
  'user.age': 20,
});
// { user: { name: 'Sam', age: 20 } }

unflattenObject({
  a: 1,
  'config.theme.color': 'blue',
});
// { a: 1, config: { theme: { color: 'blue' } } }

Constraints

  • Support mixed flat and nested keys
  • Handle deep paths
  • Treat numeric path segments as normal object keys
  • Do not mutate input object
Accepted7/7|Acceptance Rate100.0%