Design a utility bill payment system with login, linked accounts and a prepaid wallet
Company: Goldman Sachs
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a utility payment interface. Registered users can authenticate, link multiple utility accounts (electricity, mobile, gas, water, internet), view outstanding bills, and pay them from a prepaid wallet balance.
The system must enforce authentication before any payment-related action: users must `login` with the correct password before linking accounts, viewing bills, or paying. A successful payment must decrement the wallet balance and reduce the bill amount due on the linked utility account. Insufficient funds, unauthenticated access, unknown users, and unknown accounts must all be rejected gracefully with a deterministic failure indicator.
Listing results are deterministic: `get_bills` returns bills in ascending order of account id.
Implement the following methods:
- `register_user(user_id: str, password: str, initial_balance: int) -> bool`: register a new user with a password and initial wallet balance. Returns `True` on success, `False` if `user_id` already exists.
- `login(user_id: str, password: str) -> bool`: authenticate a user. Returns `True` on success, `False` if the user does not exist or the password is incorrect.
- `logout(user_id: str) -> bool`: log out the user. Returns `True` if the user was logged in, otherwise `False`.
- `link_account(user_id: str, account_id: str, utility_type: str, amount_due: int) -> bool`: link a utility account to the authenticated user. Returns `False` if the user is not logged in or `account_id` is already linked to any user.
- `get_bills(user_id: str) -> list[list]`: return all linked accounts of the authenticated user as a list of `[account_id, utility_type, amount_due]`, sorted ascending by `account_id`. Returns an empty list if the user is not logged in.
- `pay_bill(user_id: str, account_id: str, amount: int) -> bool`: pay `amount` toward the given utility account from the user's wallet. Returns `False` if the user is not logged in, the account is not linked to the user, the amount exceeds the wallet balance, or the amount exceeds the bill due.
- `get_balance(user_id: str) -> int`: return the current wallet balance of the authenticated user, or `-1` if the user is not logged in or does not exist.
Example 1:
```text
api = UtilityPaymentInterface()
api.register_user("alice", "pw1", 500) # True
api.register_user("alice", "other", 100) # False (alice already exists)
api.link_account("alice", "E100", "ELECTRICITY", 120) # False (alice is not logged in)
api.login("alice", "wrong") # False (wrong password)
api.login("alice", "pw1") # True
api.link_account("alice", "E100", "ELECTRICITY", 120) # True
api.link_account("alice", "M200", "MOBILE", 40) # True
api.get_bills("alice") # [["E100", "ELECTRICITY", 120], ["M200", "MOBILE", 40]]
api.pay_bill("alice", "E100", 100) # True
api.get_balance("alice") # 400
api.get_bills("alice") # [["E100", "ELECTRICITY", 20], ["M200", "MOBILE", 40]]
```
Linking fails until alice logs in with the right password. The payment of 100 fits both her wallet (500) and the bill (120), so the wallet drops to 400 and the electricity bill to 20.
Example 2:
```text
api = UtilityPaymentInterface()
api.register_user("bob", "b0b", 50) # True
api.register_user("carol", "c4r", 300) # True
api.login("bob", "b0b") # True
api.login("carol", "c4r") # True
api.link_account("bob", "W300", "WATER", 80) # True
api.link_account("carol", "W300", "GAS", 10) # False (W300 is already linked to bob)
api.link_account("carol", "G400", "GAS", 60) # True
api.pay_bill("bob", "W300", 70) # False (bob's wallet holds 50)
api.pay_bill("carol", "W300", 20) # False (W300 belongs to bob)
api.pay_bill("carol", "G400", 61) # False (only 60 is due)
api.pay_bill("bob", "W300", 50) # True
api.get_balance("bob") # 0
api.logout("bob") # True
api.logout("bob") # False (bob is no longer logged in)
api.get_bills("bob") # []
api.get_balance("bob") # -1
api.get_balance("dave") # -1 (unknown user)
```
Each rejected payment leaves every balance unchanged. Bob's last payment empties his wallet and leaves 30 due on W300. After logout, bob's bill and balance queries return the failure values.
```hint Rules that span users
Two rules cross user boundaries: an account id can be linked only once in the whole system, and only its owner may pay it. Decide which structure answers both questions with a single lookup.
```
```hint Failed calls and state
`pay_bill` can fail for several reasons. Make sure a rejected call leaves both the wallet and the bill exactly as they were.
```
### Constraints & Assumptions
- `1 <= number of users <= 10^4`
- `1 <= number of utility accounts per user <= 50`
- Utility type is one of `"ELECTRICITY"`, `"MOBILE"`, `"GAS"`, `"WATER"`, `"INTERNET"`.
- `0 <= amount, balance <= 10^9`
- All `user_id`, `account_id` and `password` values are non-empty strings of length at most 32.
- At most `10^5` total operations will be performed.
### Clarifying Questions to Ask
- Is a payment of `0` a valid no-op that returns `True`, or should it be rejected?
- Should `get_bills` keep listing an account after its bill is fully paid (amount due `0`)?
- Is one logged-in flag per user enough, or should a login create a separate session (for example, per device)?
- If a logged-in user calls `login` again with a wrong password, should the existing session end?
- Can the stored passwords be kept as given for this exercise, or should the design hash them?
### What a Strong Answer Covers
- A data model for users, wallets, login state and utility accounts, with the lookup each method needs.
- How authentication is checked the same way before every linking, bill and payment call, and the exact failure value each method returns.
- The complete set of `pay_bill` checks, and a guarantee that a rejected payment changes no state.
- Deterministic ordering in `get_bills` and the time and space cost of each method.
- The limits of the in-memory design: password storage, the per-user login flag, and concurrent payments.
### Follow-up Questions
- How would you replace the per-user login flag with session tokens that expire?
- Two payments from the same wallet arrive at the same time. How do you keep the balance from going negative?
- How would you add a payment history that users can browse and that supports refunds?
- What changes if each payment must be forwarded to an external utility biller that can fail after the wallet is debited?
Overview: Design an in-memory utility bill payment service where users log in, link electricity, gas, water, mobile and internet accounts, and pay bills from a prepaid wallet. It tests data modeling, enforcing authentication on every call, validating payments before changing any state, and producing deterministic output.