~/problems

Problems

Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.

Arrays & hashing

Sorting, custom keys, coordinate compression

sorted(key=...), multi-key and stable sorts, cmp_to_key, compressing coordinates.

Notes

Recognise it when: you need to order by several fields, sort by a derived value, or process events in order. Sorting is often step 1 of a greedy or two-pointer solution.

logs.sort(key=lambda r: (-r.score, r.name))         # score desc, then name asc
from functools import cmp_to_key                     # when no key works:
nums.sort(key=cmp_to_key(lambda a, b: -1 if a + b > b + a else 1))
  • Python's sort is stable. A multi-pass sort (least significant key first) works too.
  • Coordinate compression: rank = {v: i for i, v in enumerate(sorted(set(xs)))}, which maps big or sparse values to 0..k-1.
  • Negate numbers for descending order. For strings, use reverse=True or a second stable pass.

Gotchas: don't sort a list inside a loop. And sorted() returns a new list while .sort() sorts in place.

6 problems

Interview roadmap

Arrays & hashing Counting, lookups, sorting with keys, prefix sums.

Sorting, custom keys, coordinate compression guide

esc