FastPrepBank Requests with Delayed Cashback

Bank Requests with Delayed Cashback

Ramp logoRamp● MediumNEW GRADINTERNFULLTIMEOA
Learn

Problem statement

You are given the initial balances of several bank accounts and a sequence of timestamped requests. Process the requests in order and return the resulting balances.

Account holder IDs are the 1-based positions of their accounts in balances.

Request format

Every request is a lowercase space-delimited string in one of these forms:

  • deposit timestamp holderId amount: add amount to the selected account.
  • withdraw timestamp holderId amount: remove amount from the selected account and schedule a cashback equal to floor(amount * 2 / 100).

Cashback processing

A scheduled cashback is credited exactly 86400 seconds after its successful withdrawal. Before processing an external request at timestamp t, apply every cashback whose due timestamp is at most t. Therefore, a cashback due at the same timestamp as a deposit or withdrawal is applied first.

After the last external request is processed, ignore every cashback whose due timestamp is later than that request.

Invalid requests

A request is invalid when its holderId is outside 1..balances.length, or when a withdrawal exceeds the account's available balance after all cashback due at that timestamp has been applied.

Result

If every request is valid, return the final balances. Otherwise, stop at the first invalid request and return a one-element array containing the negative 1-based index of that request.

Function

processBankRequests(balances: long[], requests: String[]) → long[]

Examples

Example 1

balances = [1000,500]requests = ["withdraw 100 1 200","deposit 200 2 300","withdraw 86500 1 500"]return = [304,800]

The first withdrawal leaves account 1 at 800 and schedules cashback 4 for timestamp 86500. The deposit raises account 2 to 800. At timestamp 86500, cashback is applied first, so account 1 becomes 804 before the withdrawal leaves it at 304. The new cashback is due after the last request and is ignored.

Example 2

balances = [100]requests = ["withdraw 10 1 100","withdraw 86410 1 3"]return = [-2]

The first withdrawal schedules cashback 2. That cashback is credited before request 2 at the same timestamp, but the balance is still only 2. Withdrawing 3 is invalid, so the result is [-2].

Example 3

balances = [200,300]requests = ["deposit 5 3 10"]return = [-1]

There are only two accounts, so holder ID 3 makes the first request invalid.

Constraints

  • 1 <= balances.length <= 200000
  • 1 <= requests.length <= 200000
  • 0 <= balances[i] <= 10^9
  • Every request has exactly four space-delimited tokens and uses deposit or withdraw.
  • Every request amount is in 1..10^9.
  • Request timestamps are nonnegative and strictly increasing.
  • Every intermediate balance fits in a signed 64-bit integer.

Source note: The original slides contain an account watermark over part of the prompt. The displayed copies mask only those source-account marks without reconstructing obscured pixels.

More Ramp problems

See Ramp hiring insights
public long[] processBankRequests(long[] balances, String[] requests) {
    // write your code here
}
balances[1000,500]
requests["withdraw 100 1 200","deposit 200 2 300","withdraw 86500 1 500"]
expected[304,800]
Checking account…