Design a loan lifecycle state machine with payments, missed payments and delinquency
Company: Brex
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a state machine that manages the lifecycle of a loan. Each loan has an outstanding `balance` and a current `state`, which is one of `ACTIVE`, `DELINQUENT` or `PAID`.
A loan starts in the `ACTIVE` state with a given principal. As payments are made the balance decreases. If missed payments accumulate to the threshold `maxMissedPayments`, the loan becomes `DELINQUENT`. Once the balance reaches zero the loan moves to `PAID`, which is a terminal state: no further transitions are allowed.
Transition rules:
- `ACTIVE -> PAID` when a payment reduces the balance to 0.
- `ACTIVE -> DELINQUENT` when missed payments reach `maxMissedPayments`.
- `DELINQUENT -> ACTIVE` when a payment is made that brings missed payments back below `maxMissedPayments`. Every accepted payment resets the missed-payment count to 0.
- `DELINQUENT -> PAID` when a payment reduces the balance to 0.
- `PAID` is terminal. Any later `pay` or `missPayment` call is a no-op and returns false.
A payment larger than the outstanding balance is accepted and pays the loan off. The balance never goes below 0.
Implement a `LoanStateMachine(principal: int, maxMissedPayments: int)` class with these methods:
- `pay(amount: int) -> bool` applies a payment toward the loan. It returns true if the payment was accepted, or false if the loan is already `PAID`. It moves the loan to `PAID` when the balance reaches 0, or back to `ACTIVE` when it is recovering from `DELINQUENT`.
- `missPayment() -> bool` records a missed payment. It returns true if the miss was recorded, or false if the loan is already `PAID`. It moves an `ACTIVE` loan to `DELINQUENT` when missed payments reach `maxMissedPayments`.
- `getState() -> str` returns the current state of the loan: `"ACTIVE"`, `"DELINQUENT"` or `"PAID"`.
- `getBalance() -> int` returns the current outstanding balance.
Example 1:
```text
Input:
["LoanStateMachine", "pay", "getBalance", "getState", "pay", "getState", "getBalance"]
[[1000, 3], [400], [], [], [600], [], []]
Output: [null, true, 600, "ACTIVE", true, "PAID", 0]
loan = LoanStateMachine(1000, 3) # principal 1000, maxMissedPayments 3
loan.pay(400) # true, balance 600, state ACTIVE
loan.getBalance() # 600
loan.getState() # "ACTIVE"
loan.pay(600) # true, balance 0, state PAID
loan.getState() # "PAID" (terminal)
loan.getBalance() # 0
```
Example 2:
```text
Input:
["LoanStateMachine", "missPayment", "getState", "missPayment", "getState", "pay", "getState", "missPayment", "getState"]
[[500, 2], [], [], [], [], [100], [], [], []]
Output: [null, true, "ACTIVE", true, "DELINQUENT", true, "ACTIVE", true, "ACTIVE"]
loan = LoanStateMachine(500, 2) # principal 500, maxMissedPayments 2
loan.missPayment() # true, missed 1, state ACTIVE
loan.getState() # "ACTIVE"
loan.missPayment() # true, missed 2 >= 2, state DELINQUENT
loan.getState() # "DELINQUENT"
loan.pay(100) # true, missed reset to 0, balance 400, state ACTIVE
loan.getState() # "ACTIVE"
loan.missPayment() # true, missed 1
loan.getState() # "ACTIVE"
```
Example 3:
```text
loan = LoanStateMachine(300, 1) # principal 300, maxMissedPayments 1
loan.missPayment() # true, missed 1 >= 1, state DELINQUENT
loan.pay(500) # true, balance 0 (not -200), state PAID
loan.missPayment() # false, PAID is terminal
loan.pay(10) # false
loan.getState() # "PAID"
loan.getBalance() # 0
```
```hint Order of a payment's effects
A payment does two things: it ends any delinquency and it reduces the balance. Decide their order, then trace a delinquent loan that receives a payment larger than its balance.
```
### Constraints & Assumptions
- `1 <= principal <= 10^9`
- `1 <= maxMissedPayments <= 100`
- `1 <= amount <= 10^9` for each `pay` call
- At most `10^4` operations will be performed.
### Clarifying Questions to Ask
- Does one payment clear every missed payment, or only one of them?
- What should happen when a payment is larger than the remaining balance?
- Should a missed payment on a loan that is already delinquent change anything?
- How should a zero or negative amount be handled if it ever arrives?
- Are more states likely to be added later, so the transition logic should be easy to extend?
### What a Strong Answer Covers
- An explicit state field and transition rules, with `PAID` enforced as terminal in every method that mutates.
- The recovery rule for a payment on a delinquent loan, and the order in which the missed count and the balance are updated.
- Overpayment handling, so the balance never goes negative and one payment can move a loan from `DELINQUENT` straight to `PAID`.
- Correct behavior at exactly `maxMissedPayments`, including a threshold of 1.
- Constant time and space per operation, and a structure that stays readable as states are added.
### Follow-up Questions
- How would you add a `DEFAULTED` state reached after a second, higher missed-payment threshold, and keep the transitions easy to audit?
- How would you record a history of state transitions for auditing?
- If payments and missed-payment events reach the loan concurrently from different services, how do you keep its state consistent?
- How would the design change if each payment cleared only one missed payment?
Overview: Implement a loan lifecycle state machine with active, delinquent and paid states, driven by payments and missed payments against a delinquency threshold. It tests clear transition rules, a terminal state guard, overpayment handling and careful edge cases.