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

Binary search on the answer

Monotonic feasibility check + search the smallest value that works.

Notes

Recognise it when: the question is "minimum capacity / speed / time such that X is possible", feasibility is monotone in the answer, and checking one candidate is easy (a greedy O(n) check).

def feasible(cap):
    days, load = 1, 0
    for w in weights:
        if load + w > cap: days, load = days + 1, 0
        load += w
    return days <= D

lo, hi = max(weights), sum(weights)
while lo < hi:
    mid = (lo + hi) // 2
    if feasible(mid): hi = mid
    else: lo = mid + 1
return lo

Gotchas: pick bounds that are definitely infeasible or feasible (lo = the tightest lower bound). Use integer ceiling division, -(-a // b). For "maximum such that", flip the logic.

11 problems

Interview roadmap

esc