DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
MediumJavaScript

Your Template Engine Safely Accesses Deeply Nested Config Values Without Crashing on Missing Keys

0 views

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:

  1. Path Parsing: The path can be a string using dot notation (a.b.c) or bracket notation for arrays (a[0].b).

  2. Null Safety: The function must handle null or undefined input objects gracefully without throwing an error.

  3. Defaulting: If any segment of the path does not exist, the function should immediately bail and return the provided defaultValue.

  4. Complex Keys: Assume the object may contain both nested objects and arrays.


Example Use Cases

Example 1: Standard Deep Access

JavaScript

const config = { settings: { theme: { color: "blue" } } };
get(config, "settings.theme.color", "red"); 
// Expected Output: "blue"

Example 2: Array Indexing

JavaScript

const data = { users: [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }] };
get(data, "users[1].name", "Unknown"); 
// Expected Output: "Bob"

Example 3: Missing Path with Default

JavaScript

const user = { profile: { email: "rahul@google.com" } };
get(user, "profile.phone.mobile", "No Phone Provided"); 
// Expected Output: "No Phone Provided"

Example 4: Handling Null Root

JavaScript

get(null, "any.path", 0); 
// Expected Output: 0

Interview Evaluation Criteria

  • Regex/Parsing Strategy: How do you normalize the path string? (e.g., converting a[0].b into a consistent array of keys like ['a', '0', 'b']).

  • Iterative vs. Recursive: Can you implement this without causing a stack overflow on extremely deep objects?

  • Falsy Value Handling: Does your function correctly return false or 0 if those are the actual values at the path, or does it accidentally return the defaultValue?

  • Performance: Are you re-parsing the string on every step, or splitting it once at the start?

Sample Test Cases

Case 1
Input
obj={a:{b:{c:42}}}, path="a.b.c"
Expected Output
42
Case 2
Input
path="a.b.d", default="N/A"
Expected Output
"N/A"
Case 3
Input
obj={a:[{b:1}]}, path="a[0].b"
Expected Output
1

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
0
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering

Languages

JavaScript