Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Extract a Nested Key From Arrays
00:00
5 left

Extract a Nested Key From Arrays

EasyPython

Problem

Würth Industry North America's VMI inventory data is grouped into nested arrays, where each element is a Python dictionary describing one catalog item. Given the nested records, an item identifier, and a dot-separated key path, return the corresponding value from the first matching object.

The key path may reference a top-level key, such as price, or nested dictionaries, such as dimensions.length. Return None if no object has the requested identifier or if any part of the key path is missing. Values may legitimately be None, and should be returned unchanged when the complete path exists.

Formal Specification

Implement get_catalog_value(records, item_id, key_path), where records is a list of rows, each row is a list of dictionaries. Every dictionary contains an item_id key, and identifiers are unique across all rows. Each key-path component refers only to dictionary keys.

Return the value at key_path for the object whose item_id equals item_id, or None when the object or path does not exist.

Constraints

  • 1 <= len(records) <= 10^4
  • Each row contains at least one dictionary
  • The total number of dictionaries is at most 10^5
  • 1 <= len(key_path.split(".")) <= 10
  • Every dictionary contains an item_id key
  • Item identifiers are unique across all rows

Function Signature

def get_catalog_value(records, item_id, key_path):
Interviewer

Your question is Extract a Nested Key From Arrays. 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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.