Technical Interview Challenge: The "Safe-Path" Configuration Resolver
Context
In large-scale microfrontend architectures or complex CMS template engines, applications often consume deeply nested JSON configurations. A common runtime failure occurs when code attempts to access a property on an undefined or null intermediate key (the classic Cannot read property 'x' of undefined).
While modern JavaScript offers Optional Chaining (?.), many template engines and legacy utility libraries require a robust, string-based path resolver that can handle dynamic paths and provide fallback defaults to ensure the UI never crashes due to a malformed configuration.
Problem Statement
Implement a utility function get(obj, path, defaultValue) that retrieves a value from an object at a specified nested path. If the resolved value is undefined or if the path is unreachable, return the defaultValue.
Requirements:
Path Parsing: The
pathcan be a string using dot notation (a.b.c) or bracket notation for arrays (a[0].b).Null Safety: The function must handle
nullorundefinedinput objects gracefully without throwing an error.Defaulting: If any segment of the path does not exist, the function should immediately bail and return the provided
defaultValue.Complex Keys: Assume the object may contain both nested objects and arrays.
Example Use Cases
Example 1: Standard Deep Access
JavaScript
Example 2: Array Indexing
JavaScript
Example 3: Missing Path with Default
JavaScript
Example 4: Handling Null Root
JavaScript