Ledger Replay: Deposits, Withdrawals and Percentage Rewards
Data Structure DesignAuto-graded · 20 test casesLast updated
Simulate a single account balance across a sequence of operations, silently rejecting invalid ones, and report the balance after each step.
You are given a starting account balance and a list of operations to run against it, in order. Each operation has a name and a numeric amount. Return the account balance observed after each operation.
Inputs
start_balance(float): the balance before any operation runs.actions(list of strings): the operation names.values(list of floats):values[i]is the amount foractions[i].
Operations
| Name | Allowed when | Effect on the balance |
|---|---|---|
"Deposit" | value >= 0 | add value |
"Withdraw" | value >= 0 and value <= balance | subtract value |
"CashBack" | value >= 0 | add balance * value / 100.0 (value is a percentage of the current balance) |
Rules
- If an operation's condition is not met, it is rejected. A rejected operation leaves the balance unchanged and does not raise an error.
- After every processed operation, accepted or rejected, append the current balance to the result. The result therefore has one entry per processed operation.
- Operations are paired with amounts by position. If the two lists have different lengths, only the first
min(len(actions), len(values))pairs are processed and the rest are ignored. - If an operation name is anything other than the three above, raise
ValueError. - Use ordinary floating-point arithmetic with no rounding, and apply the formulas exactly as written above.
- Withdrawing exactly the current balance is allowed and leaves
0. - A zero amount is valid for every operation, subject to the conditions in the table.
- Order matters, because
CashBackuses the balance at the moment it runs.
Return a list of floats (or numbers) with the balance after each processed operation.
Constraints
0 <= len(actions), len(values) <= 10^5- Amounts are finite numbers and may be negative (such amounts are rejected).
- Operation names are case-sensitive strings.
- Do not round results.
Examples
Example 1
Input: replay_account_actions(100, ['Deposit', 'Withdraw', 'CashBack'], [50, 30, 10])
Output: [150, 120, 132]
Example 2
Input: replay_account_actions(100, ['Withdraw', 'Deposit', 'Withdraw'], [150, 50, 100])
Output: [100, 150, 50]
Hints
Hint 1
Only one value changes over time: the current balance. Walk through the pairs of (action, value) in order and keep updating it.
Hint 2
Decide validity before changing anything. A rejected action must still add the unchanged balance to the output so the output length matches the number of processed pairs.
Hint 3
Use `<=` when comparing a withdrawal to the balance so the full balance can be withdrawn, divide the cashback percentage by 100, and raise `ValueError` for any unrecognized name.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The whole problem is a simulation with one piece of state, the current balance. Iterate over the paired (action, value) entries in order. For each pair:
- Identify the action. If it is not one of the three known names, raise
ValueError. - Check the validity condition for that action.
- If the condition holds, update the balance with the matching formula. Otherwise leave it as is.
- Append the balance to the output list, whether or not the operation was applied.
Why it is correct
After processing the first k pairs, balance equals the start balance with exactly the valid operations among those k applied in order. This holds before any operation (base case), and each step either applies the documented change or leaves the balance alone (inductive step). The balance is then appended exactly once per pair, so the output has the right length and values.
Overdrafts are prevented by the guard value <= balance on withdrawals. Using <= rather than < allows withdrawing the entire balance. Cashback is a percentage of the balance at that moment, so the order of operations affects the result. zip naturally stops at the shorter list, which matches the rule for mismatched lengths.
Complexity
- Time: O(n), where n is the number of processed pairs, since each step is constant work.
- Space: O(n) for the output list, plus O(1) extra state.
Reference solution
from typing import List
def replay_account_actions(start_balance: float, actions: List[str], values: List[float]) -> List[float]:
balance = start_balance
history = []
for action, value in zip(actions, values):
if action == "Deposit":
if value >= 0:
balance += value
elif action == "Withdraw":
if value >= 0 and value <= balance:
balance -= value
elif action == "CashBack":
if value >= 0:
balance += balance * value / 100.0
else:
raise ValueError("Unknown operation")
history.append(balance)
return history