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:
dict[str, Any]).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.
/ as the separator./ (e.g., root/a.txt, not /root/a.txt)./.tree: dict[str, Any]dict[str, Any] where each key is a full path and each value is the original metadata.Input
{
"root": {
"docs": {"readme.md": 1200},
"bin": {"app": {"main.py": "sha256:abc"}},
"empty": {},
"license.txt": null
}
}
Output
{
"root/docs/readme.md": 1200,
"root/bin/app/main.py": "sha256:abc",
"root/license.txt": null
}
Explanation: Only leaf nodes that are files are emitted; empty is ignored.
Input
{"a": {"b": {"c.txt": 1}}, "x.log": 0}
Output
{"a/b/c.txt": 1, "x.log": 0}
Explanation: The function collects paths from multiple roots.
1 <= number_of_nodes <= 2 * 10^5<= 10^4/