KatInterview

Build a Social Payment Platform with Friend Connections

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented-designhash-tablesstate-management

Last updated

Implement an in-memory system that handles user registration, friendship management, and peer-to-peer money transfers with proper validation.

You're building the core logic for a social payment platform. Your system must handle user accounts, friendship connections, and money transfers between friends.

Implement a class that supports these operations:

1. User Registration

  • Create a new user account with a unique identifier and starting balance
  • Each user tracks their current balance and their list of friends

2. Friendship Management

  • Users can propose a friendship connection
  • The proposal must be explicitly accepted before the friendship becomes active
  • Each proposal gets a unique identifier
  • When accepting, you reference the original proposal's ID (not the usernames again)

3. Money Transfers

  • Users can send money to their friends
  • Transfers are only valid between users who have an active friendship
  • The sender must have sufficient funds

Constraints:

  • All identifiers (for proposals, acceptances, and transfers) must be unique across the entire system
  • Users cannot befriend themselves or send money to themselves
  • Friendship is bidirectional—if A befriends B, then B is also friends with A
  • A transfer fails if: users don't exist, they aren't friends, or the sender lacks funds
  • Proposals cannot be sent to someone who is already a friend
  • Each proposal can only be accepted once
  • All balances are non-negative integers (representing smallest currency units)
  • Transfer amounts must be positive

Your interface should support:

class SocialPaymentPlatform:
    def add_user(self, user_id: str, starting_balance: int) -> bool:
        # Returns False if user_id already exists or starting_balance is invalid
        
    def propose_friendship(self, proposal_id: str, user_a: str, user_b: str) -> bool:
        # Returns False if users don't exist, proposal_id is reused, or they're already friends
        
    def confirm_friendship(self, confirmation_id: str, original_proposal_id: str) -> bool:
        # Returns False if proposal doesn't exist, was already confirmed, or IDs are invalid
        
    def send_money(self, transaction_id: str, sender: str, receiver: str, amount: int) -> bool:
        # Returns False if users aren't friends, insufficient balance, or invalid parameters

Design your data structures to efficiently enforce all validation rules. Document your assumptions about edge cases.

Hints

Hint 1

Store friendship proposals separately from active friendships. The acceptance step is what creates the actual friendship relationship.

Hint 2

Use multiple hash maps: one for user objects, one for pending proposals, and a global set to track all used identifiers.

Hint 3

Validate everything before making any state changes. This ensures each operation is atomic—it either fully succeeds or leaves the state unchanged.