Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Flatten Nested Directory JSON Paths
00:00
5 left

Flatten Nested Directory JSON Paths

EasyPython

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):
Interviewer

Your question is Flatten Nested Directory JSON Paths. 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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.