17/29
Part 4 of 6 · Sorting and the latest entryLearn and do

A prefix scan is a filter

Level 2's scan returns the fields that start with a prefix.

Prefixappleapricotfigpearplum
apyesyes
emptyyesyesyesyesyes

A prefix scan lists the keys that start with a given string: [k for k in sorted(d) if k.startswith(prefix)]. Every string starts with the empty string. So the full listing is the same code with prefix="", and one method serves both.

Sorting on every call costs O(n log n), which is fine at test sizes. After the sort, the matches sit next to each other. A faster version uses that, under What interviewers ask next.

Predict

A statement is built straight from list(self._balances.items()). The accounts were opened zoe, ana, ben. What order comes out?

Pick an answer above to see the rest