Design a basic credit card system
AmazonSoftware Engineer · Technical ScreenLast updated
This problem tests object-oriented modeling and state management by requiring the design of a class structure that handles credit limits, transaction authorization, and balance tracking.
Problem Statement
Design a simplified Credit Card processing system that models a user's credit account. Your system should be able to track a credit limit, the current balance, and a history of transactions.
Requirements
CreditCard: Should store cardholder information, the credit limit, and the current outstanding balance.Transaction: Should store an amount, a timestamp, and the status (PENDING, APPROVED, DECLINED).ProcessTransaction(amount): A method to attempt a transaction. A transaction should be declined if the amount exceeds the remaining credit (limit - current balance).GetStatement(): Should return a list of all successful transactions for the current billing cycle.
Constraints
- The balance cannot exceed the credit limit.
- Ensure thread safety if multiple transactions occur simultaneously (conceptual).
- Maintain a clean separation between the user account state and the transaction logs.
Example
# Input:
card = CreditCard(limit=1000)
card.process_transaction(500) # Returns True
card.process_transaction(600) # Returns False (exceeds limit)
# Output:
# card.get_balance() -> 500
# card.get_statement() -> [Transaction(500, Approved)]
Edge Cases
- Transaction amount is zero or negative.
- A transaction exactly equal to the remaining credit limit.
- Concurrent attempts to exceed the credit limit.
Hints
Hint 1
Use a composition pattern where the CreditCard class maintains a list of Transaction objects to separate entity responsibilities.
Hint 2
Consider using an enum or a constants class for transaction statuses to improve readability and maintainability.
Hint 3
Think about using a mutex or a synchronized block if you need to ensure the balance calculation is atomic.
Follow-up questions
- How would you modify your design to support daily transaction limits in addition to the total credit limit?
- If this system needs to scale to millions of users, how would you handle database sharding for these transaction records?
- How would you implement a 'Refund' operation that updates the balance and adds a new transaction entry?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.