Problem, as an interviewer would state it:
"Implement traverseTree(obj) — recursively walking a nested plain-object tree and collecting every LEAF value together with its own full dot-notation path."
Examples:
traverseTree({a: 1, b: {c: 2}});
// [{path: "a", value: 1}, {path: "b.c", value: 2}]
Clarifying questions expected:
- Should a real ARRAY value be treated as a leaf itself, or recursed into like a nested object — since arrays are genuinely objects too in JavaScript?
- What real, correct format should the path take — a dot-separated string, or an array of keys?
- Does the traversal need to visit keys in any particular real order (e.g. matching
Object.keys's own order)?
Code / implementation expected: Yes — real, direct proof against a genuinely nested, multi-level tree, confirming correct leaf detection including the real array-as-leaf edge case.