Pergunta de entrevista da empresa Nutanix

Round 1: 1. Find first non repeating character in a string. Eg: “ababcdab” -> c 2. Flatten object: Convert this: { name: "Alex", address: { city: "Karnal", pincode: "132001", }, hobbies: ["reading", "writing"], education: { courses: ["abc", "xyz"], } } To: { name: “Alex”, address.city: "Karnal", address.pincode: 132001, hobbies[0]: "reading", hobbies[1]: "writing", education.courses[0]: "abc", education.courses[1]: "xyz", }

Resposta da entrevista

Sigiloso

17 de nov. de 2025

const obj = { name: "Mrigu", address: { city: "BLR", pincode: "560048", }, hobbies: ["reading", "writing"], education: { courses: ["AI", "JAVA"], } } let flatObj={}; function flat(item, parentKey="") { for(key in item) { let newKey = parentKey ? `${parentKey}.${key}` : key; if(Array.isArray(item[key])) { item[key].forEach((val,i)=> { flatObj[`${newKey}[${i}]`] = val; }) } else if(typeof item[key]==='object') { flat(item[key], newKey); } else { flatObj[newKey] = item[key]; } } } flat(obj); console.log(flatObj);