Read a nested setting

dictionaries

Read a nested setting

Mastercard Python Interview Question

Mastercard's fraud rules are stored as nested dictionaries. Engineers look up a setting with a dotted path such as "limits.daily.amount".

Write a function get_setting(settings, path) that follows the path one key at a time and returns the value it finds. Return None if any key along the path is missing or leads to something that is not a dictionary.

Asked of

  • Data Analyst
  • Data Engineer
  • Data Scientist
  • ML Engineer
  • AI Engineer

Example 1

Input

settings = {"limits": {"daily": {"amount": 5000, "count": 20}}, "region": "US"}, path = "limits.daily.amount"

Output

5000

Example 2

Input

settings = {"limits": {"daily": {"amount": 5000}}}, path = "limits.weekly.amount"

Output

None

Explanation

In the first example, "limits" leads to a dictionary, "daily" leads to another, and "amount" holds 5000. In the second example, there is no "weekly" key under "limits", so the answer is None.

Submit also runs 4 hidden test cases that check edge cases.