注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
Cloud Compute Credits System
Implement a simplified cloud compute credits system.
All operations have a timestamp parameter representing a timestamp in milliseconds.
All timestamps are unique.
1 <= timestamp <= 10^9
Operations are given in strictly increasing timestamp order.
Level 1 — Workspace Management and Credit Transfers
Initially, the system contains no workspaces.
The system should support creating workspaces, adding credits to workspaces, and transferring credits between workspaces.
boolean createWorkspace(int timestamp, String workspaceId)
Creates a new workspace with the given identifier.
Requirements:
The newly created workspace has an initial credit balance of 0.
Return true if the workspace is successfully created.
Return false if a workspace with the same workspaceId already exists.
Optional<Integer> topUp(int timestamp, String workspaceId, int amount)
Adds amount credits to the specified workspace.
Requirements:
If workspaceId does not exist, return Optional.empty().
Otherwise, add amount to the workspace's balance.
Return the workspace's balance after the top-up.
Optional<Integer> transferCredits(int timestamp, String sourceWorkspaceId, String targetWorkspaceId, int amount)
Transfers amount credits from the source workspace to the target workspace.
Return Optional.empty() if:
sourceWorkspaceId does not exist.
targetWorkspaceId does not exist.
sourceWorkspaceId and targetWorkspaceId are the same.
The source workspace has insufficient credits.
Otherwise:
Deduct amount from the source workspace.
Add amount to the target workspace.
Return the source workspace's remaining balance.
Level 2 — Rank Workspaces by Outgoing Credits
The platform wants to identify workspaces that drain their credits quickly.
The system should support ranking workspaces according to their total outgoing credit transactions.
List, 20) → 900
If no operation happened exactly at timeAt, return the balance resulting from the most recent operation before timeAt.
Workspace existence at timeAt
If the specified workspace did not exist at timeAt, return:
Optional.empty()
This applies in two situations.
If queried before creation:
t=10 create workspaceA
getCredits(..., "workspaceA", 5)
→ Optional.empty()
If queried after that workspace was merged away:
t=10 create workspaceB
t=30 merge B into A
getCredits(..., "workspaceB", 20)
→ B's balance at t=20
getCredits(..., "workspaceB", 30)
→ Optional.empty()
getCredits(..., "workspaceB", 40)
→ Optional.empty()
So even though workspaceB is no longer active, its pre-merge historical balance must remain queryable.
Merge and balance history
A merge affects the surviving workspace starting at the merge timestamp, not retroactively.
For example:
t=10:
A = 1700
B = 600
t=20:
merge B into A
Then:
getCredits(..., "A", 15)
→ 1700
getCredits(..., "B", 15)
→ 600
getCredits(..., "A", 20)
→ 2300
getCredits(..., "B", 20)
→ Optional.empty()
This distinction is especially important: B's old balance must not be added retroactively to A's pre-merge history. |