I recently faced a challenging coding question during an interview with Paytm, and I'm curious about how others might approach it. The task was to transform a flat object with dot-separated keys into a properly nested object or array structure. For example, given an input where keys like 'skills.domain.fe.0' and 'projects.0.title' exist, the desired output would include a well-organized structure where attributes are nested appropriately, such as 'skills' and 'projects' containing arrays and nested objects. I'm looking for strategies that could help solve this kind of problem quickly during interviews!
4 Answers
For a job-related scenario, using a library like Normalizr could be beneficial. It handles data normalization effortlessly if you find yourself repeatedly facing similar structures in interviews.
This kind of problem can seem tricky at first, primarily because of the arrays! But honestly, once you get the hang of recursion, it becomes more straightforward. Just navigate through the keys and split them at the dots to build the nested structure.
I’d say it’s not overly complicated. While I’d typically give a junior developer about an hour for this, with practice, you can definitely get it done in 20 minutes. Just remember to keep track of the current level in your nested structure!
If you want to practice more, check out this platform for coding challenges: [https://ebat.dev/frontend/javascript/challenges/convert-flat-object-keys-into-nested-structure-LxcJGFEIvTLTkXqZyPvue](https://ebat.dev/frontend/javascript/challenges/convert-flat-object-keys-into-nested-structure-LxcJGFEIvTLTkXqZyPvue). It has similar problems that can help you get used to the format!

Yeah, I agree! Recursion can really simplify the process for nesting objects. It's all about breaking it down!