NewProject Coding is here — build features and fix bugs in real codebases with an AI assistantSee what's new

Building on the previous question, extend the existing LoanProcessor so that it can match an incoming transaction to one of the loans created earlier.

Add the following method to LoanProcessor:

  • String matchTransaction(List<String> transaction) processes one transaction and returns the matching loan ID, or an empty string if no match exists.

    • Each transaction has the format [transactionId, userId, merchant, amount]. The transactionId identifies the transaction and does not participate in matching.

    • A transaction matches a stored loan only when all of the following conditions meet:

      • Their userId values are equal.
      • Their amount strings are equal.
      • Their merchants resolve to the same top-level company using the rules from the previous question.
    • Only loans created before the current matchTransaction call are eligible.

    • If the merchant is empty, the transaction cannot match any loan.

    • Matching does not remove or modify a loan, and more than one transaction may match the same loan.

The input guarantees that no two non-ignored stored loans have the same combination of userId, top-level company, and amount, so a transaction matches at most one loan.

Constraints:

  • All constraints from the previous question remain in effect.
  • Each transaction contains four strings.
  • transactionId, userId, and amount are non-empty; merchant may be empty.
  • Amount strings are compared exactly as provided.

Example:

Input: ["LoanProcessor", "createLoan", "createLoan", "createLoan", "matchTransaction", "matchTransaction", "matchTransaction", "matchTransaction", "matchTransaction", "matchTransaction"] [[[["AA", "BB"], ["AA", "CC"], ["DD", "AA"]]], [["L1", "U1", "CC", "100"]], [["L2", "U2", "AA", "200"]], [["L3", "U1", "EE", "50"]], [["T1", "U1", "BB", "100"]], [["T2", "U2", "DD", "200"]], [["T3", "U1", "CC", "999"]], [["T4", "U1", "", "50"]], [["T5", "U1", "EE", "50"]], [["T6", "U1", "AA", "100"]]]

Output: [null, null, null, null, "L1", "L2", "", "", "L3", "L1"]

Explanation:

  • processor = new LoanProcessor(companyRelations);
  • processor.createLoan(["L1", "U1", "CC", "100"]);
  • processor.createLoan(["L2", "U2", "AA", "200"]);
  • processor.createLoan(["L3", "U1", "EE", "50"]);
  • processor.matchTransaction(["T1", "U1", "BB", "100"]); // Returns "L1", because "BB" and "CC" resolve to "DD".
  • processor.matchTransaction(["T2", "U2", "DD", "200"]); // Returns "L2", because "AA" resolves to "DD".
  • processor.matchTransaction(["T3", "U1", "CC", "999"]); // Returns "", because the amount differs.
  • processor.matchTransaction(["T4", "U1", "", "50"]); // Returns "", because the merchant is empty.
  • processor.matchTransaction(["T5", "U1", "EE", "50"]); // Returns "L3", because "EE" resolves to itself.
  • processor.matchTransaction(["T6", "U1", "AA", "100"]); // Returns "L1". Matching does not consume the loan.

Accepted
407/1.1k
Acceptance Rate36.2%
Test Cases
Input
Output
Expected