Skip to main content

Problem Statement

Design an online chat system that supports user management, friend connections, private messaging, and group chat functionality.

Constraints and Assumptions

  • Text conversations only (no media)
  • User operations:
    • Add, remove, update users
    • Manage friend lists
    • Send, approve, reject friend requests
  • Chat types:
    • Private 1-1 chat
    • Group chat with multiple participants
  • No scaling concerns initially (single server)

Design Overview

The chat system uses several interconnected classes:
  1. UserService: Manages all users and friend requests
  2. User: Represents a chat user with friends and conversations
  3. Chat: Abstract base class for all chat types
  4. PrivateChat: One-on-one conversation
  5. GroupChat: Multi-user conversation
  6. Message: Individual message in a chat
  7. AddRequest: Friend request with status tracking
  8. RequestStatus: Enum for friend request states
This design uses the Mediator pattern where UserService acts as a central coordinator for user interactions, and the Composite pattern where different chat types share a common interface.

Implementation

UserService Class

Central service for managing users and relationships:

User Class

Represents a user with all their social connections:

Chat Hierarchy

Abstract base class and concrete implementations:

Message Class

Represents individual messages:

Friend Request Classes

Manage friend connection requests:

Key Design Patterns

Mediator Pattern

UserService acts as a mediator coordinating interactions between users:

Composite Pattern

Different chat types share a common interface:

Observer Pattern (Implicit)

Users maintain references to their chats and can be notified of new messages:
  • friend_ids_to_private_chats: Maps friends to conversations
  • group_chats_by_id: Tracks group memberships

System Workflows

Friend Request Flow

Private Chat Flow

Group Chat Flow

Data Structure Design

User Relationships

The User class uses multiple dictionaries to efficiently manage relationships:

Complexity Analysis

Most operations are O(1) due to hash map lookups. The exception is remove_user(), which requires iterating through friends to remove bidirectional references.

Design Considerations

Advantages

  • Clear separation: Users, chats, and messages are distinct entities
  • Bidirectional relationships: Both users maintain friend and chat references
  • Flexible chat types: Easy to add new chat types (e.g., channels, threads)
  • Request lifecycle: Clear states for friend requests

Friend Request States

Potential Improvements

  1. Message delivery: Add read receipts, delivery status, typing indicators
  2. Online status: Track user presence (online, away, offline)
  3. Message history: Implement pagination for loading older messages
  4. Search: Full-text search across messages and users
  5. Notifications: Push notifications for new messages and friend requests
  6. Encryption: End-to-end encryption for private messages
  7. Media support: Images, videos, files, voice messages
  8. Message reactions: Emojis, likes, replies
  9. User blocking: Prevent unwanted interactions
  10. Admin controls: Moderation tools for group chats
  11. Scalability: Distribute users across servers, use message queues
  12. Persistence: Database backend for storing users and messages
  13. WebSocket support: Real-time message delivery
  14. Rate limiting: Prevent spam and abuse