~/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.

Binary search

Sorted containers (bisect)

Ordered-set operations in Python: bisect.insort, floor/ceiling lookups.

Notes

Recognise it when: you need an ordered set or map with inserts plus "floor / ceiling / previous / next" queries: calendars, leaderboards, the nearest element.

import bisect
starts = []                            # keep sorted
i = bisect.bisect_right(starts, x)     # floor = starts[i-1], ceiling = starts[i]
bisect.insort(starts, x)               # O(n) insert, but fast in practice
  • sortedcontainers.SortedList gives O(log n) inserts, but it's not in the standard library and may not be allowed. Know the bisect fallback.
  • Calendar booking: find the neighbours around the new start and check that they don't overlap.

Gotchas: keep parallel lists in sync, or store tuples. insort is O(n), which is fine up to about 10^5 operations.

11 problems

Interview roadmap

esc