Problem · Hash Table

Account Scheduler - Availability and Acquire

Learn this problem
MediumStripe logoStripeFULLTIMEONSITE INTERVIEW
See Stripe hiring insights

Problem statement

You manage a set of accounts that can be locked until a given time. Process a sequence of time-stamped queries and report availability.

Setup: accountIds is an int[] of account ids. lockedUntil is a String[] where each entry is "accountId,timestamp" giving the time each account is initially locked until (an account not listed is unlocked, i.e. locked until 0).

An account is available at time t when its lock time is <= t (i.e. t >= lockedUntil). Queries arrive in order, with no concurrency:

  • IS_AVAILABLE <accountId> <t>: output true if the account is available at time t, else false.
  • ACQUIRE <accountId> <duration> <t>: lock the account for duration time units starting at the query time t, i.e. set its lock time to t + duration. Output OK.

Return a String[] with one output line per query, in order.

(Follow-up discussed in the source but not part of this contract: an ACQUIRE with no account id that auto-picks the least-recently-used available account; this draft covers the deterministic explicit-id availability and acquire behavior.)

Function

processScheduler(accountIds: int[], lockedUntil: String[], queries: String[]) → String[]

Examples

Example 1

accountIds = [1, 2, 3, 4]lockedUntil = ["1,10", "2,5", "3,0", "4,20"]queries = ["IS_AVAILABLE 1 8", "IS_AVAILABLE 2 8", "IS_AVAILABLE 3 1", "IS_AVAILABLE 4 21"]return = ["false", "true", "true", "true"]
Account 1 is locked until 10, so at t=8 it is not available -> false. Account 2 locked until 5, at t=8 available -> true. Account 3 locked until 0, at t=1 available -> true. Account 4 locked until 20, at t=21 available -> true.

Example 2

accountIds = [1, 2, 3, 4]lockedUntil = ["1,10", "2,5", "3,0", "4,20"]queries = ["IS_AVAILABLE 3 1", "ACQUIRE 3 5 1", "IS_AVAILABLE 3 4", "IS_AVAILABLE 3 6"]return = ["true", "OK", "false", "true"]
At t=1 account 3 (locked until 0) is available -> true. ACQUIRE 3 for duration 5 at t=1 locks it until 1+5=6 -> OK. At t=4, 4 < 6 so it is not available -> false. At t=6, 6 >= 6 so it is available again -> true.

Constraints

  • lockedUntil entries are "accountId,timestamp"; unlisted accounts are locked until 0.
  • An account is available at t iff t >= lockedUntil.
  • ACQUIRE accountId duration t sets the lock time to t + duration and outputs OK.
  • Queries are processed in order with no concurrency; output one line per query.

More Stripe problems

drafts saved locally
public String[] processScheduler(int[] accountIds, String[] lockedUntil, String[] queries) {
  // write your code here
}
accountIds[1, 2, 3, 4]
lockedUntil["1,10", "2,5", "3,0", "4,20"]
queries["IS_AVAILABLE 1 8", "IS_AVAILABLE 2 8", "IS_AVAILABLE 3 1", "IS_AVAILABLE 4 21"]
expected["false", "true", "true", "true"]
checking account