Code RoomFlatten nested dict
MediumPrep Room Coding #1249

Flatten nested dict

CodingAlgorithms & data structuresMid–Senior~30 min

Flatten a nested dict into a single-level dict whose keys are dotted paths. Given a dict whose values are either scalars (int, str, bool) or nested dicts (arbitrarily deep), produce a flat dict mapping each leaf's full dotted key path to its scalar value. For example {'a': {'b': 1}} becomes {'a.b': 1}. An empty dict (at any level) contributes no keys. Return the flattened dict.

Implement
flatten_dict(d: dict) → dict
Examples
in[{"a":{"b":1,"c":2},"d":3}]out{"d":3,"a.b":1,"a.c":2}
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 30 min
InputExpectedGot
[{"a":{"b":1,"c":2},"d":3}]{"d":3,"a.b":1,"a.c":2}not run yetsample