Skip to the page

Utility functions · mid

get/set by path, classnames and key casing

Many utility questions are small recursive walks over objects: read or write a value by a path like 'a[0].b', build a class string from mixed arguments (classnames), convert keys between camelCase and snake_case, or replace function values with their results — always returning new data instead of mutating the input.

The notes

What you must know

  • Path parsing: 'a[0].b.c' -> ['a', '0', 'b', 'c'] with path.replace(/[(\w+)]/g, '.$1').split('.'), or a regex match. Also accept an array path.
  • get(obj, path, fallback): walk while the current value isn't null or undefined; return fallback only when the result is undefined (lodash returns null for a stored null).
  • set(obj, path, value): create missing containers — an array when the next key is numeric, else an object. lodash mutates; an immutable version copies each object along the path.
  • classnames(...args): add strings and numbers, flatten arrays recursively, add object keys whose values are truthy, ignore falsy values.
  • Key casing: recurse into plain objects and arrays; camel = s.replace(/([a-z])/g, (, c) => c.toUpperCase()).
  • Resolve function values (Flipkart 2025): recurse; if a value is a function, replace it with its result; the async version awaits with Promise.all.
  • Group and aggregate (Livspace 2025): group with reduce or Map, sum durations, dedupe with Set, then sort.

The red pen

Common traps: mistake → fix

  • Mistake: Returning the fallback when the value is null or 0

    Fix: only undefined should fall back.

  • Mistake: Mutating the input in set or casing utilities when the question expects new data.

  • Mistake: Treating arrays as plain objects in recursive walks.

Easy to hard

Questions interviewers ask, easy to hard

  1. Implement lodash get. (Quizizz, Jan 2025)
  2. Implement set that creates missing objects and arrays.
  3. Implement classnames. (Meta)
  4. Convert all keys to camelCase, deeply.
  5. Replace every function value in a nested object with its return value; then the async version. (Flipkart 2025)

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview