← Questions

Account scheduler with locking and LRU selection

0
StripeCodingLast asked Dec 2025

A coding interview question asked in Stripe's technical phone screen, last asked Dec 2025. It tests scheduling, locks and lru.

Build AccountScheduler, which hands out accounts from a fixed pool for periods of time. Each account is locked until some time and free from then on. Callers can check an account, lock a specific one, or ask for any free account, in which case the scheduler picks the least recently used one. Calls are sequential and their timestamps never go backwards.

Part 1: availability

  • AccountScheduler(accounts: list[int], locked_until: dict[int, int]) — an account is locked for every t < locked_until[id] and free for t >= locked_until[id]. Accounts missing from the dict start free (0).
  • is_available(account_id, t) -> bool

Part 2: acquire a given account

  • acquire(account_id, t, duration) -> bool — if the account is free at t, lock it for [t, t + duration) and return True; otherwise return False and change nothing. A successful acquire counts as a use at time t.

Part 3: acquire any account, least recently used first

  • auto_acquire(t, duration) -> int | None — among the accounts free at t, pick the one whose last successful acquire is oldest. Never-used accounts count as oldest; break remaining ties by the smaller id. Lock it as in Part 2 and return its id, or None if none is free.

Example

s = AccountScheduler([1, 2, 3], {1: 5})
s.auto_acquire(0, 10)   ->  2       # 2 and 3 are free and never used; 2 is smaller
s.auto_acquire(2, 3)    ->  3       # locked until 5
s.is_available(1, 4)    ->  False
s.acquire(1, 5, 4)      ->  True    # locked until 9, used at 5
s.auto_acquire(6, 1)    ->  3       # only 3 is free (since 5)
s.auto_acquire(10, 1)   ->  2       # 1, 2, 3 free; 2 was used longest ago (at 0)
s.acquire(3, 10, 2)     ->  True
s.auto_acquire(10, 5)   ->  1

Constraints

  • Up to 10^5 accounts and 10^5 operations; duration >= 1.

Clarifying questions

  • Should acquire on a locked account fail, or extend the lock from its current end?
  • How should ties among never-used accounts be broken?
  • Is there a release that frees an account early?
  • Do I need to handle concurrent callers, or are calls strictly sequential?

What a strong answer covers

  • A dict for lock ends and a dict for last use; is_available and acquire in O(1).
  • For auto_acquire, avoiding a full scan: a min-heap of locked accounts by unlock time, moved into a heap of free accounts keyed by (used?, last_used, id) as time passes, with lazy deletion of stale entries; O(log n) amortized.
  • Exact boundary handling (t == locked_until is free) and tie-breaking.
  • Tests that replay sequences mixing all three operations.

Follow-ups

  • Add release(account_id, t) to free an account before its lock ends.
  • Make it safe for concurrent callers in one process, then across several machines.
  • Accounts have weights or per-account rate limits.

Where it's asked

  • Stripe · Technical phone screen · Last asked Dec 202560 min · One realistic task in parts, in a project editor (CoderPad project mode or HackerRank Projects) with several files and tests

What it tests

scheduling · locks · lru

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding