# Deep Get

> Nested deep. Reach in and grab it.

Canonical URL: <https://datadriven.io/problems/deep_get>

Domain: Python · Difficulty: medium · Seniority: L4

## Problem

Given a possibly-nested dict d and a list of keys, traverse d[keys[0]][keys[1]]... step by step. Return the value found at that path, or None if any intermediate key is missing.

## Worked solution and explanation

### Why this problem exists in real interviews

Safely traversing nested dictionaries by a key path is a real-world utility function used in every config parser and API response handler. It tests **iterative dict traversal** and **graceful failure on missing keys**.

---

### Break down the requirements

#### Step 1: Start at the root dict

The current reference begins at the top-level dictionary.

#### Step 2: Walk the key path one level at a time

For each key in the path, attempt to descend into the next level of nesting.

#### Step 3: Return None if any key is missing or the value is not a dict

If a key does not exist or the current value is not a dict when more keys remain, return None.

---

### The solution

**Iterative key-path traversal with safe fallback**

```python
def nested_access(d, keys):
    current = d
    for key in keys:
        if not isinstance(current, dict) or key not in current:
            return None
        current = current[key]
    return current
```

> **Time and Space Complexity**
>
> **Time:** O(k) where k is the length of the key path. Each key lookup is O(1) average for a dict.
> 
> **Space:** O(1). Only a single reference variable is updated.

> **Interviewers Watch For**
>
> Checking `isinstance(current, dict)` before indexing. If an intermediate value is a string or integer, `current[key]` would not raise KeyError but would behave unexpectedly.

> **Common Pitfall**
>
> Using a try/except around `current[key]` without checking the type. This catches KeyError but misses TypeError when current is not a dict, leading to confusing error handling.

---

## Common follow-up questions

- How would you support a default value instead of None? _(Tests adding a default parameter to the function signature.)_
- What if the path should support list indices like 'items.0.name'? _(Tests extending traversal to handle both dict key access and list index access.)_
- How would you implement deep_set to create the path if it does not exist? _(Tests building nested dicts on the fly with setdefault chaining.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/deep_get)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.