Transform flat keys into nested dictionary
NetflixMachine Learning Engineer · Technical ScreenLast updated
This question evaluates proficiency in parsing and restructuring hierarchical parameter keys into nested dictionary structures, string manipulation for grouping, handling nonconforming keys and duplicates, and reasoning about time and space complexity.
You are given a flat collection of parameter keys like ['layer1.attention.q_proj.weight', 'layer1.attention.k_proj.weight', 'layer1.mlp.fc1.weight', ...]. Write a Python function that converts this into a nested dictionary grouped by layer index, placing entries that share the same component path under the same layer (e.g., attention, mlp). Define your grouping rule (e.g., strip the 'layer{n}.' prefix to identify similar components), handle nonconforming keys and duplicates, and analyze time and space complexity.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.