Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Sorting proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Expose useful order with an explicit comparison key, while choosing stability, mutation and memory deliberately. Compare full ordering with counting, selection and top-k heaps instead of treating one sort as every task.
6 practice exercises10 interview problems
Recognize it when Grouping equal values makes a later adjacent scan possible.
Checking your account…
Sorting pays O(n log n) once to expose adjacency, rank, sweep order, or duplicate groups. After ordering, a problem that required repeated global comparison can often be solved with one linear scan.
Encode every priority in one tuple: negative numeric fields for descending order, positive fields for ascending order, and a deterministic final tie-breaker. A key function is easier to audit than a custom comparator.
def rank_key(record):
name, score, attempts = record
return (-score, attempts, name)Implement rank_key(record). record is [name, score, attempts]. Return a tuple key that sorts higher scores first, fewer attempts next, then names alphabetically.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def rank_key(record):
name, score, attempts = record
return (-score, attempts, name)Call the existing key Lab once to inspect a record, then use that same function with sorted. This makes the three priorities visible before any sorting machinery runs.
| Step | Executed statement | State | Why |
|---|---|---|---|
| Unpack ivy | name, score, attempts = record |
name='ivy', score=8, attempts=2 |
Each position has one ranking role. |
| Encode ivy | return (-score, attempts, name) |
(-8, 2, 'ivy') |
Smaller negative scores represent higher original scores. |
| Encode ada | return (-score, attempts, name) |
(-8, 2, 'ada') |
The complete key differs from ivy only in the last tie-breaker. |
| Encode neo | return (-score, attempts, name) |
(-9, 4, 'neo') |
The primary score outranks the larger attempt count. |
| Sort complete keys | sorted([['ivy', 8, 2], ['ada', 8, 2], ['neo', 9, 4]], key=rank_key) |
[['neo', 9, 4], ['ada', 8, 2], ['ivy', 8, 2]] |
Sorting compares the returned tuples lexicographically; the return statement alone does not sort records. |
| Output | |
|---|---|
print(rank_key(['ivy', 8, 2])) |
(-8, 2, 'ivy') |
print(sorted([['ivy', 8, 2], ['ada', 8, 2], ['neo', 9, 4]], key=rank_key)) |
[['neo', 9, 4], ['ada', 8, 2], ['ivy', 8, 2]] |
Stability does not repair a wrong key. Explicit tie priorities should be in the key; original tie order is retained only when the complete keys compare equal.
Python sorting is stable: equal keys retain their original relative order. sorted() returns a new list, while list.sort() mutates and returns None. Choose deliberately so an interview helper does not unexpectedly destroy caller-owned order.
def stable_group(records):
return sorted(records, key=lambda record: record[0])Implement stable_group(records). records contains [group, value] pairs. Return a new list sorted by group while preserving original order within equal groups.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def stable_group(records):
return sorted(records, key=lambda record: record[0])Comparison sorting is the default for arbitrary values. Counting or buckets can reach O(n + k) when the key domain is small and known, but costs O(k) memory. A heap is preferable when only the top k items matter and k is much smaller than n.
Say: “I sort by this explicit tuple key, then scan adjacent items under this invariant.” State whether stability matters, whether mutation is allowed, and include the linear post-pass in the O(n log n) bound. The Python list reference(opens in a new tab) defines sort, and Car Fleet(opens in a new tab) demonstrates sorting to reveal processing order.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Sorting proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Python comparison sort | O(n log n) | O(n log n) | Can use existing runs; key extraction is once per record, with its own cost. |
| Counting over k keys | O(n + k) | O(n + k) | Requires a small bounded integer domain. |
O(n) worst-case auxiliary storage for Python sorting; sorted also returns a new list.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
reverse=True makes names descend as well as scores. The contract wants high score but alphabetical ties.
Prevent it: Negate only descending numeric fields and retain ascending tie fields.
ordered = sorted(records, key=lambda r: (r[1], r[0]), reverse=True)ordered = sorted(records, key=lambda r: (-r[1], r[0]))Where you will hit this: Rank Top Words(opens in a new tab)
list.sort mutates the list and returns None. Assigning that result loses the list reference needed next.
Prevent it: Use sorted for a new result or call sort as a separate mutating statement.
ordered = values.sort()ordered = sorted(values)Where you will hit this: Rank Top Words(opens in a new tab)
Sorting [group,label] pairs without a key orders labels inside equal groups, destroying the requested original tie order.
Prevent it: Sort by group only when stability must retain input order inside each group.
ordered = sorted(records)ordered = sorted(records, key=lambda r: r[0])Where you will hit this: Car Fleet(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27
python-docs · checked 2026-07-12