Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Every job reads its configuration from three places: a defaults file checked into the repo, an environment file per deployment, and flags passed at run time. Today they are combined with dict.update, which means setting one field of the warehouse section deletes every other field in it.
Write merge_config(layers). The layers arrive lowest precedence first.
Function to write
merge_config(layers: list[dict]) -> dictOne merged configuration dict, with later layers overriding earlier ones at every depth.
How to approach it
Write the two-layer merge first and fold the layers through it; the nesting is the recursion.
Sample cases
+ 2 held back until you submit
defaults, environment, flags
A nested section is overridden by two layers, a null does not override, and a list is replaced whole.
Input
Argument 1
[
{
'warehouse': {
'host': 'localhost',
'port': 5432,
'schema': 'public'
},
'retries': 3,
'tags': [
'default'
],
'debug': False
},
{
'warehouse': {
'host': 'warehouse.internal',
'ssl': True
},
'retries': None,
'tags': [
'prod',
'eu'
]
},
{
'warehouse': {
'port': 6543
},
'debug': True
}
]Returns
{
'warehouse': {
'host': 'warehouse.internal',
'port': 6543,
'schema': 'public',
'ssl': True
},
'retries': 3,
'tags': [
'prod',
'eu'
],
'debug': True
}no layers
Nothing to merge produces an empty configuration rather than None.
Input
Argument 1
[] (empty list)
Returns
{}only defaults
A single layer comes back as itself, so the merge is safe to run with nothing to override.
Input
Argument 1
[
{
'warehouse': {
'host': 'localhost',
'port': 5432,
'schema': 'public'
},
'retries': 3,
'tags': [
'default'
],
'debug': False
}
]Returns
{
'warehouse': {
'host': 'localhost',
'port': 5432,
'schema': 'public'
},
'retries': 3,
'tags': [
'default'
],
'debug': False
}Constraints
None means the layer has no opinion. It does not override, and it does not add the key.Worked example
The defaults set the warehouse to localhost on port 5432 with schema public. The environment layer sets only the host, and the flags layer sets only the port. Merged, the warehouse keeps public as its schema — with dict.update it would have been dropped twice over, and the job would connect to whatever schema the driver defaults to.
retries is the subtler rule. The environment layer sets it to None, which is how a YAML file spells a key somebody left blank. Treating that as an override replaces a working value of 3 with nothing, and the job stops retrying at all — a change nobody made on purpose and nobody can see in a diff of the merged result.
What this tests
Recursion with a clear base case, and the difference between a value that is absent and a value that is null. Configuration is where that distinction has consequences, because a blank line in a file is not the same as a decision.
merge_config(layers: list[dict]) -> dictSubmit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.