Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Flatten Nested Directory JSON Paths

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Flatten Nested Directory JSON Paths. Start with the requirements on the right.

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.

Problem

A cybersecurity team at a healthcare SaaS provider scans customer “export bundles” before they’re uploaded to cloud storage. Each bundle is represented as a nested JSON-like object (Python dict) describing a directory tree. With millions of daily exports, the scanner needs a fast way to flatten this structure into a list of full file paths so downstream services can apply allow/deny rules and compute checksums.

You are given a nested object where:

  • Directories are represented by dictionaries (dict[str, Any]).
  • Files are represented by non-dictionary values (e.g., None, integers, strings). The value is metadata you should preserve.

Implement flatten_directory(tree) that returns a flat dictionary mapping each full path to the corresponding file metadata.

Path rules

  1. Use / as the separator.
  2. The root has no leading / (e.g., root/a.txt, not /root/a.txt).
  3. Only files appear in the output; empty directories contribute nothing.
  4. Keys in the input do not contain /.

Formal signature

  • Input: tree: dict[str, Any]
  • Output: dict[str, Any] where each key is a full path and each value is the original metadata.

Notes

  • Your solution should be efficient for large trees.
  • Avoid recursion depth issues in Python for deeply nested inputs.

Constraints

  • 1 <= nodes <= 2 * 10^5
  • depth <= 10^4
  • Keys are non-empty strings and do not contain '/'
  • Any non-dict JSON-serializable value (treated as opaque)

Function Signature

def _flatten_directory_test_adapter(**kwargs):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output