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
To tell whether a record has changed since the last load, you store a digest of its contents and compare. The digest has to depend on what the record says and on nothing else — not the order the producer wrote the keys in, and not which process computed it.
Write digest_records(records, length). It returns one hex digest per record, truncated to length characters.
Function to write
digest_records(records: list[dict], length: int) -> list[str]A list of hex digests, one per record, truncated to `length` characters.
How to approach it
Hash the same record twice with its keys in a different order.
Sample cases
+ 2 held back until you submit
the same record, two key orders
Identical contents must produce identical digests — key order is not content.
Input
Argument 1
[
{
'amount': 12.5,
'buyer': 'amir'
},
{
'buyer': 'amir',
'amount': 12.5
}
]Argument 2
12Returns
[
'2a0de924c96b',
'2a0de924c96b'
]a nested record
Nesting is not special-cased: the whole structure has to be encoded canonically.
Input
Argument 1
[
{
'user': {
'id': 'u1',
'geo': {
'city': 'Pune',
'country': 'IN'
}
}
}
]Argument 2
12Returns
[
'a89337923b47'
]no records
An empty batch produces an empty list rather than an error or a digest of nothing.
Input
Argument 1
[] (empty list)
Argument 2
12Returns
[] (empty list)
Constraints
length hex characters long.None is different from a key that is absent, and the digests must differ.hashlib.sha256, so the digest is the same in every process and every run.Worked example
{'amount': 12.5, 'buyer': 'amir'} and {'buyer': 'amir', 'amount': 12.5} are the same record written twice, so both must produce the same digest.
The starter hashes str(record), which renders the dict in insertion order — so the two produce different digests and every record looks changed on the next load. Python's built-in hash() has the opposite problem: it is randomised per process, so it is stable within one run and useless between two.
What this tests
That a digest is only as stable as its encoding, and that a canonical encoding is a decision you make rather than a property you get. It is also the practical reason json.dumps(sort_keys=True) turns up in every change-detection pipeline.
digest_records(records: list[dict], length: int) -> list[str]Submit 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.