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
The orders feed is newline-delimited JSON, and a producer only writes a field when it has a value. The warehouse table has fixed columns, so the loader has to turn that ragged stream into rows of one shape — and the team also wants to know when a producer starts sending something nobody declared.
Write align_rows(rows, columns). Return the aligned rows and the undeclared fields you found.
Function to write
align_rows(rows: list[dict], columns: list[str]) -> dictA dict with rows — every row holding exactly the declared columns — and unexpected, the undeclared field names.
How to approach it
Build each output row by walking the columns, never by walking the row.
Sample cases
+ 2 held back until you submit
a ragged file
One row is missing qty, one is missing sku, and two carry fields the schema never declared.
Input
Argument 1
[
{
'order_id': 'o1',
'sku': 'A1',
'qty': 2,
'price': 9.99
},
{
'order_id': 'o2',
'sku': 'B2',
'price': 5
},
{
'order_id': 'o3',
'sku': 'C3',
'qty': 1,
'price': 1.5,
'coupon': 'SAVE10'
},
{
'order_id': 'o4',
'qty': 4,
'price': 2,
'channel': 'app',
'coupon': 'SAVE10'
}
]Argument 2
[
'order_id',
'sku',
'qty',
'price'
]Returns
{
'rows': [
{
'order_id': 'o1',
'sku': 'A1',
'qty': 2,
'price': 9.99
},
{
'order_id': 'o2',
'sku': 'B2',
'qty': None,
'price': 5
},
{
'order_id': 'o3',
'sku': 'C3',
'qty': 1,
'price': 1.5
},
{
'order_id': 'o4',
'sku': None,
'qty': 4,
'price': 2
}
],
'unexpected': [
'coupon',
'channel'
]
}no rows
An empty file still produces both keys, with an empty list under each.
Input
Argument 1
[] (empty list)
Argument 2
[
'order_id',
'sku',
'qty',
'price'
]Returns
{
'rows': [],
'unexpected': []
}everything already matches
A row that already fits comes back unchanged, and nothing is reported as unexpected.
Input
Argument 1
| order_id | sku | qty | price |
|---|---|---|---|
| o9 | Z9 | 1 | 3 |
Argument 2
[
'order_id',
'sku',
'qty',
'price'
]Returns
{
'rows': [
{
'order_id': 'o9',
'sku': 'Z9',
'qty': 1,
'price': 3
}
],
'unexpected': []
}Constraints
None. A missing field is an absent value, not an absent column.unexpected.rows and unexpected. Both keys are always present, including on an empty file.Worked example
Row o2 carries no qty, so the aligned row holds None there. That is different from a zero, and the difference is the whole reason the column exists: nobody ordered zero of something, the producer simply did not say.
SAVE10 arrives under a coupon field on two different rows. It is reported once, not twice, because unexpected is a description of the feed rather than a log of the rows. A loader that reports it per row buries the one useful signal — that a producer is sending a field the warehouse is silently discarding — under thousands of identical lines.
What this tests
That the schema decides the shape and the data only reports. Iterating the row to build the output is the natural move and it is what produces ragged rows; iterating the declared columns is what makes every row identical.
align_rows(rows: list[dict], columns: list[str]) -> 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.