Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Writing a Utility Function
00:00
5 left

Writing a Utility Function

EasyPython

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

Your question is Writing a Utility Function. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

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.