Design in-memory payment and friendship system
Problem: In-Memory Payment and Friendship System
Design and implement an in-memory payment system with simple social features. The system must process a sequence of requests, including:
- Register a user: Create a new user account.
- Send a friend request: Initiate a friendship between two existing users.
- Accept a friend request: Finalize a friendship based on a previously sent request.
- Transfer money: Move funds between two users.
Rules and Requirements
-
Users and Balances
- Each user has a unique
user_id(string), abalance(integer amount, e.g., cents), and a list or set offriends(other user IDs).
- Each user has a unique
-
Friend Requests
- A friend request is initiated with a unique
req_id,user1, anduser2. Bothuser1anduser2must be existing users. - An
accept-friendrequest contains its own uniquereq_idand thereq_idof the original friend request to accept. - Users become friends only when an
accept-friendrequest is successfully processed.
- A friend request is initiated with a unique
-
Transfers
- Money can only be transferred between users who are already friends.
- A transfer request specifies a unique
req_id,from_user,to_user, and anamount(must be > 0). - A transfer must fail if:
- Either user does not exist.
- The users are not friends.
from_userdoes not have sufficient balance.
- On success,
amountis subtracted fromfrom_user.balanceand added toto_user.balance.
-
Error Handling
- If a request is invalid (e.g., refers to an unknown user, a non-existent friend request ID, attempts to accept a friend request twice), the system should indicate failure for that request.
- Define and document reasonable behavior for repeated or conflicting requests.
Interface
Design classes and methods using an object-oriented approach. A possible interface (names/signatures can be adjusted for clarity) is provided below:
class PaymentSystem:
def register_user(self, user_id: str, initial_balance: int) -> bool:
"""Register a new user with given balance. Return False if user_id already exists."""
def send_friend_request(self, req_id: str, user1: str, user2: str) -> bool:
"""Store a friend request identified by req_id. Return False on invalid input or duplicate req_id."""
def accept_friend_request(self, req_id: str, original_req_id: str) -> bool:
"""Accept the previously stored friend request with ID original_req_id. Return False if original_req_id doesn't exist, is already accepted, or users are invalid."""
def transfer(self, req_id: str, from_user: str, to_user: str, amount: int) -> bool:
"""Transfer amount from from_user to to_user if they are friends and balance is sufficient. Return True on success, False otherwise."""
Implementation Details
- Maintain all necessary internal state in memory (no external database required).
- Enforce the friendship and balance constraints for transfers.
- Correctly handle the indirection where a friend-accept request refers to an earlier friend-request by its
req_id. - You may ignore concurrency in the basic solution, but you can optionally comment on how you would make operations atomic under multiple threads.
AI interview prep
Core dilemma
Ensuring strict data integrity and atomicity for financial transactions and complex state transitions (like friend requests) within a single-process, in-memory system, while maintaining efficient access patterns for user and friendship data.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design in-memory payment and friendship system
Problem: In-Memory Payment and Friendship System
Design and implement an in-memory payment system with simple social features. The system must process a sequence of requests, including:
- Register a user: Create a new user account.
- Send a friend request: Initiate a friendship between two existing users.
- Accept a friend request: Finalize a friendship based on a previously sent request.
- Transfer money: Move funds between two users.
Rules and Requirements
-
Users and Balances
- Each user has a unique
user_id(string), abalance(integer amount, e.g., cents), and a list or set offriends(other user IDs).
- Each user has a unique
-
Friend Requests
- A friend request is initiated with a unique
req_id,user1, anduser2. Bothuser1anduser2must be existing users. - An
accept-friendrequest contains its own uniquereq_idand thereq_idof the original friend request to accept. - Users become friends only when an
accept-friendrequest is successfully processed.
- A friend request is initiated with a unique
-
Transfers
- Money can only be transferred between users who are already friends.
- A transfer request specifies a unique
req_id,from_user,to_user, and anamount(must be > 0). - A transfer must fail if:
- Either user does not exist.
- The users are not friends.
from_userdoes not have sufficient balance.
- On success,
amountis subtracted fromfrom_user.balanceand added toto_user.balance.
-
Error Handling
- If a request is invalid (e.g., refers to an unknown user, a non-existent friend request ID, attempts to accept a friend request twice), the system should indicate failure for that request.
- Define and document reasonable behavior for repeated or conflicting requests.
Interface
Design classes and methods using an object-oriented approach. A possible interface (names/signatures can be adjusted for clarity) is provided below:
class PaymentSystem:
def register_user(self, user_id: str, initial_balance: int) -> bool:
"""Register a new user with given balance. Return False if user_id already exists."""
def send_friend_request(self, req_id: str, user1: str, user2: str) -> bool:
"""Store a friend request identified by req_id. Return False on invalid input or duplicate req_id."""
def accept_friend_request(self, req_id: str, original_req_id: str) -> bool:
"""Accept the previously stored friend request with ID original_req_id. Return False if original_req_id doesn't exist, is already accepted, or users are invalid."""
def transfer(self, req_id: str, from_user: str, to_user: str, amount: int) -> bool:
"""Transfer amount from from_user to to_user if they are friends and balance is sufficient. Return True on success, False otherwise."""
Implementation Details
- Maintain all necessary internal state in memory (no external database required).
- Enforce the friendship and balance constraints for transfers.
- Correctly handle the indirection where a friend-accept request refers to an earlier friend-request by its
req_id. - You may ignore concurrency in the basic solution, but you can optionally comment on how you would make operations atomic under multiple threads.