# The Deep Selector

> Tell it what you want. It knows where to look.

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

Domain: Python · Difficulty: easy · Seniority: L4

## Problem

Given a nested dict and a list of dot-notation paths, return a dict mapping each path to the value at that path (traversing each segment as a dict key). Use None for paths that do not fully resolve.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **nested dict traversal using dot-notation paths**, a pattern common in configuration systems, JSON APIs, and feature stores. It probes safe navigation through nested structures with graceful handling of missing keys.

---

### Break down the requirements

#### Step 1: Split each path by dots

A path like `'a.b.c'` becomes `['a', 'b', 'c']`, representing nested keys.

#### Step 2: Navigate the dict following each key segment

Walk into the nested structure one key at a time. If any key is missing, the value is `None`.

#### Step 3: Build the output dict

Map each original dot-path to the resolved value or `None`.

---

### The solution

**Dot-path navigation with safe key access**

```python
def select_fields(data: dict, paths: list) -> dict:
    result = {}
    for path in paths:
        keys = path.split('.')
        current = data
        found = True
        for key in keys:
            if isinstance(current, dict) and key in current:
                current = current[key]
            else:
                found = False
                break
        if found:
            result[path] = current
        else:
            result[path] = None
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(p * d) where p is the number of paths and d is the average depth per path.
> 
> **Space:** O(p) for the output dict.

> **Interviewers Watch For**
>
> Whether you check `isinstance(current, dict)` before accessing keys. Navigating into a non-dict value (like a string or int) would raise an error.

> **Common Pitfall**
>
> Using `try/except KeyError` instead of explicit checks. While it works, it masks bugs when the path partially exists but hits a non-dict intermediate value.

---

## Common follow-up questions

- What if paths could use bracket notation for list indices? _(Tests parsing mixed dot and bracket notation like `a.b[0].c`.)_
- How would you set a value at a dot-separated path? _(Tests creating intermediate dicts as needed during traversal.)_
- What if the same prefix is accessed many times? _(Tests caching or trie-based optimization for shared path prefixes.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_deep_selector)
- [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.