FastPrepFriendship and Balance Tracker

Friendship and Balance Tracker

Robinhood logoRobinhood● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Process API-like operations in order. Each begins with a unique request id.

  • id REGISTER user balance creates a user.
  • id REQUEST from to creates a pending friend request stored under this id.
  • id ACCEPT requestId accepts and consumes that pending request.
  • id TRANSFER from to amount moves money only between accepted friends when the sender has enough balance.

Return id OK for a successful non-transfer, id OK senderBalance receiverBalance for a successful transfer, or id REJECTED. Rejected operations do not change state.

Function

processFriendshipTransfers(operations: String[]) → String[]

Examples

Example 1

operations = ["1 REGISTER a 100","2 REGISTER b 0","3 REQUEST a b","4 ACCEPT 3","5 TRANSFER a b 30"]return = ["1 OK","2 OK","3 OK","4 OK","5 OK 70 30"]

The accepted friendship authorizes the affordable transfer.

Example 2

operations = ["r1 REGISTER a 10","r2 REGISTER b 0","r3 TRANSFER a b 1"]return = ["r1 OK","r2 OK","r3 REJECTED"]

Registered users are not friends by default.

Example 3

operations = ["1 REGISTER a 5","2 REGISTER a 9"]return = ["1 OK","2 REJECTED"]

Duplicate registration is rejected without replacing the balance.

Constraints

  • 1 <= operations.length <= 2 * 10^5.
  • Ids and usernames contain no spaces; request ids are unique.
  • Balances and amounts fit signed 64-bit integers.

More Robinhood problems

See Robinhood hiring insights
public String[] processFriendshipTransfers(String[] operations) {
    // Write your solution here.
}
operations["1 REGISTER a 100","2 REGISTER b 0","3 REQUEST a b","4 ACCEPT 3","5 TRANSFER a b 30"]
expected["1 OK", "2 OK", "3 OK", "4 OK", "5 OK 70 30"]
Checking account…