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
A join in the nightly pipeline reads a product dimension into memory and looks each fact row up by SKU. It is written as a one-line dict comprehension, which means a SKU that appears twice silently keeps whichever row happened to come last in the file, and nobody has ever seen the message about it because there is not one.
Write build_index(rows, key_field). Return the lookup and what it had to skip.
Function to write
build_index(rows: list[dict], key_field: str) -> dictA dict with index — the keyed lookup — plus duplicates, the repeated keys with their row counts, and skipped.
How to approach it
Decide what a usable key is first; the duplicate report falls out of checking before you assign.
Sample cases
+ 2 held back until you submit
a dimension with problems
One key appears three times, one row has no key at all, and one has a key of only spaces.
Input
Argument 1
[
{
'sku': 'A1',
'name': 'Widget',
'price': 9.99
},
{
'sku': 'B2',
'name': 'Gadget',
'price': 5
},
{
'sku': 'A1',
'name': 'Widget v2',
'price': 11.5
},
{
'name': 'Nameless',
'price': 1
},
{
'sku': ' ',
'name': 'Blank',
'price': 2
},
{
'sku': 'A1',
'name': 'Widget v3',
'price': 12
}
]Argument 2
'sku'Returns
{
'index': {
'A1': {
'sku': 'A1',
'name': 'Widget',
'price': 9.99
},
'B2': {
'sku': 'B2',
'name': 'Gadget',
'price': 5
}
},
'duplicates': [
{
'key': 'A1',
'rows': 3
}
],
'skipped': 2
}an empty table
Nothing to index, so the index is empty and nothing is reported as skipped.
Input
Argument 1
[] (empty list)
Argument 2
'sku'Returns
{
'index': {},
'duplicates': [],
'skipped': 0
}a clean dimension
Every row indexes, so duplicates is empty and skipped is zero.
Input
Argument 1
| sku | name |
|---|---|
| X1 | Clean |
| X2 | Also clean |
Argument 2
'sku'Returns
{
'index': {
'X1': {
'sku': 'X1',
'name': 'Clean'
},
'X2': {
'sku': 'X2',
'name': 'Also clean'
}
},
'duplicates': [],
'skipped': 0
}Constraints
key_field. The first row for a key wins.skipped.0 is a real key. Only null and blank count as missing.duplicates, each as a dict with the key and rows — the total number of rows carrying it, including the one that was kept.index, duplicates and skipped. All three keys are always present.Worked example
A1 appears three times, as Widget, Widget v2 and Widget v3. The index keeps Widget, and duplicates records A1 with 3 rows — so a fact row joining to A1 gets a price of 9.99 and somebody can see that the answer was a choice rather than the only possibility.
The dict comprehension everyone writes first — one line, keyed by SKU — keeps Widget v3 instead, because a later assignment to the same key overwrites the earlier one. Both answers are defensible; what is not defensible is that the comprehension makes the choice invisible, so the join silently changes meaning the day somebody sorts the file differently.
What this tests
That building a lookup is a data-quality decision, not a syntax exercise. The difference between the one-liner and this is not correctness on clean input — it is whether the pipeline can tell you the input was not clean.
build_index(rows: list[dict], key_field: 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.