Question Bank
Object Transformation Challenges
Difficulty: Medium
Six object-shape problems: building a tree from a flat array, valid-key extraction, chainable methods, stats reshaping, recursive leaf walking, and deep equality.
Object Transformation Challenges
Six object-shape problems: building a tree from a flat array, valid-key extraction, chainable methods, stats reshaping, recursive leaf walking, and deep equality.
575 views
2
Implement transformData(data) that turns a flat list of { id, name, parentFeatureId, level } into a map keyed by top-level feature name, each holding { parent, features: [...children] }.
Examples
Example 1:
Input: transformData([{ id: 1, name: 'Feature#A', parentFeatureId: 0, level: 1 }, { id: 2, name: 'Feature#A-1', parentFeatureId: 1, level: 2 }])
Output: { 'Feature#A': { parent: { id: 1, ... }, features: [{ id: 2, ... }] } }
Explanation: Pick parents (parentFeatureId === 0), then group children by their parent id.Implement validKeys(obj) returning the keys whose value is truthy.
Examples
Example 1:
Input: validKeys({ basics: true, intermediate: false, advanced: true })
Output: ['basics', 'advanced']
Explanation: `Object.entries` plus a filter on the value gives a one-liner.Make obj.getA().getB() log 1 then 2 by adjusting getA to return this. Don't reformat anything else.
Examples
Example 1:
Input: obj.getA().getB()
Output: 1, then 2
Explanation: Methods that return `this` enable fluent chaining (jQuery, Mantine builders, Knex).Transform a nested stats object so each top-level key holds an array of { name, total } pairs.
Examples
Example 1:
Input: transformStats({ JS: { array: 30, regex: 5 } })
Output: { JS: [{ name: 'array', total: 30 }, { name: 'regex', total: 5 }] }
Explanation: Walk Object.entries twice: once for outer keys, once for inner key/value pairs.Write parseObject(obj, callback) that recursively walks obj and invokes callback({ [key]: value }) for every leaf (non-object) property.
Examples
Example 1:
Input: parseObject({ a: 1, b: { c: 2 } }, kv => result.push(kv))
Output: result === [{ a: 1 }, { c: 2 }]
Explanation: Recurse into nested objects; invoke the callback only when the value is not an object.Implement isEqual(obj1, obj2) performing a deep, key-order-independent comparison of two plain objects.
Examples
Example 1:
Input: isEqual({ a: { b: 1 } }, { a: { b: 1 } }); isEqual({ a: 1 }, { a: 2 })
Output: true, false
Explanation: Compare key counts; recursively compare each key's value; primitives compared with strict equality.