MediumPro challengePythonJavaScriptTypeScript

Flatten Object

ObjectsRecursion

Implement solve(obj, separator) that flattens a deeply nested plain object
into a single-level object using separator to join keys.

Example

solve({ a: { b: { c: 1 }, d: 2 }, e: 3 }, '.')
// → { 'a.b.c': 1, 'a.d': 2, 'e': 3 }

Constraints

  • Only flatten plain objects (not arrays or other types).
  • Leaf values remain unchanged.
  • An empty object flattens to {}.

Sample tests

Test #1Empty object
Input: [{},"."]
Output: {}
Test #2Already flat — no separator added
Input: [{"x":1,"y":2},"/"]
Output: {"x":1,"y":2}
Test #3Four levels deep
Input: [{"a":{"b":{"c":{"d":42}}}},"__"]
Output: {"a__b__c__d":42}
Test #4Mixed depth with custom separator
Input: [{"a":1,"b":{"c":2},"d":{"e":{"f":3}}},"-"]
Output: {"a":1,"b-c":2,"d-e-f":3}
Test #5Standard nested object
Input: [{"a":{"b":{"c":1},"d":2},"e":3},"."]
Output: {"e":3,"a.d":2,"a.b.c":1}