Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Resolve Dot and Bracket Object Paths
00:00
5 left

Resolve Dot and Bracket Object Paths

MediumPython

Problem

The Teachable Creator Dashboard receives nested configuration objects, and frontend code often needs to safely read a deeply nested value. Implement a Python equivalent of Lodash's get function.

Given a nested object, a property path, and an optional default value, return the value at that path. The path may be a dot-separated string such as user.profile.name, a bracket-index string such as courses[0].title, or a list of path segments such as ["courses", 0, "title"]. Return the default value only when the path cannot be fully resolved.

For this problem, string paths use non-empty property names and non-negative integer list indexes. Bracket notation does not contain quoted property names.

Formal Specification

Implement lodash_get(obj, path, default_value). obj is a nested combination of dictionaries, lists, strings, numbers, booleans, and None. path is either a string or a list containing strings and integers. Return the resolved value, or default_value if any segment is missing or invalid. A resolved None value is valid and must not be replaced by the default.

Constraints

  • 0 <= number of path segments <= 100
  • Nested objects contain at most 10^4 total values
  • List indexes are non-negative integers
  • A missing path and a resolved None must be distinguished
  • String paths use dot notation and unquoted bracket notation such as items[0].name

Function Signature

def lodash_get(obj, path, default_value):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.