Problem · Array

Credit Card Command Processor

Learn this problem
MediumChime logoChimeFULLTIMEPHONE SCREEN

Problem statement

Process a batch of credit-card commands. Each cardholder is added at most once and starts with balance $0.

CommandBehavior
["Add", name, cardNumber, "$limit"]Create the account. A card is valid only when it has 12 to 16 digits and passes the Luhn check.
["Charge", name, "$amount"]Increase a valid card's balance unless the new balance would exceed its limit. Ignore the command for an invalid card.
["Credit", name, "$amount"]Decrease a valid card's balance. Negative balances are allowed. Ignore the command for an invalid card.

Return one row per cardholder, sorted by name. A valid card returns [name, "$balance"]; an invalid card returns [name, "error"].

Function

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

Examples

Example 1

operations = [["Add","Tom","4111111111111111","$1000"],["Add","Lisa","5454545454545454","$3000"],["Add","Quincy","12345678901234","$2000"],["Charge","Tom","$500"],["Charge","Tom","$800"],["Charge","Lisa","$7"],["Credit","Lisa","$100"],["Credit","Quincy","$200"]]return = [["Lisa","$-93"],["Quincy","error"],["Tom","$500"]]

Tom's second charge is declined because it would exceed his limit. Lisa's credit makes her balance negative, and Quincy's invalid card reports an error.

Example 2

operations = [["Add","Amy","4012888888881881","$100"],["Add","Zed","123456789012","$50"],["Charge","Amy","$100"],["Charge","Amy","$1"],["Credit","Amy","$25"],["Credit","Zed","$10"]]return = [["Amy","$75"],["Zed","error"]]

Amy reaches her limit, the next charge is ignored, and a later credit lowers the balance. Zed's invalid card ignores its credit.

Constraints

  • 1 <= operations.length <= 100000.
  • Every operation has one of the documented shapes, and every referenced name was added earlier.
  • Names contain between 1 and 50 ASCII letters and are unique.
  • Limits and amounts are decimal integers between 0 and 10^9 prefixed by $.
  • Every valid account balance fits in a signed 64-bit integer.
drafts saved locally
public String[][] processCardCommands(String[][] operations) {
    // Write your code here.
}
operations[["Add","Tom","4111111111111111","$1000"],["Add","Lisa","5454545454545454","$3000"],["Add","Quincy","12345678901234","$2000"],["Charge","Tom","$500"],["Charge","Tom","$800"],["Charge","Lisa","$7"],["Credit","Lisa","$100"],["Credit","Quincy","$200"]]
expected[["Lisa", "$-93"], ["Quincy", "error"], ["Tom", "$500"]]
checking account