Design a class AccountScheduler that manages the time-based availability of a fixed set of account IDs.
You are given:
accounts
: a list of integers representing all account IDs in the system.
locked_until
: a dictionary mapping
account_id
→ integer timestamp, e.g.:
Interpretation of locked_until[account_id]:
t
such that
t < locked_until[account_id]
.
t
such that
t >= locked_until[account_id]
.
locked_until
, treat it as if
locked_until[account_id] = 0
(initially available at
t >= 0
).
All time values are integers. You may assume all method calls are processed sequentially; you do not need to handle concurrency.
Example state and queries:
Implement the class constructor and a method:
that returns whether account_id is available at time t according to the locked_until rules above.
You may store and update internal state in any way you like, but is_available must be correct for any sequence of calls.
Extend AccountScheduler with a method to acquire/lock a specific account for some duration starting at a given time. Add:
Semantics:
t
is the current time of the operation.
duration
is a positive integer.
acquire(account_id, t, duration)
is called, you should update internal state so that the account becomes
unavailable
for the time interval
[t, t + duration)
. In terms of the
locked_until
model, this means:
(i.e., if the account was already locked until some time in the future, the new lock extends from the later of the current lock and
t
).
acquire
returns
void
or a boolean indicating success/failure; if you support failure, it should fail (e.g., return
False
) when the account is not available at time
t
.
Assume all acquire and is_available calls are supplied with non-decreasing timestamps (t never goes backwards).
Now extend the API to support acquiring any available account automatically, using an LRU (least recently used) policy.
Add an overloaded method:
Semantics:
t
(i.e.,
is_available(account_id, t)
is
True
).
acquire(account_id, t, duration)
or
auto_acquire(t, duration)
).
[t, t + duration)
as in Part 2.
account_id
.
t
, return
None
(or a sentinel value indicating failure).
Your task:
is_available(account_id, t)
acquire(account_id, t, duration)
auto_acquire(t, duration)