InterviewDB Experience

Chat Service: Implement a Multi-Room Chat Backend with Message History

Interview Experience

Round 1 Coding

Problem

Implement a backend for a multi-room chat service. Users can join rooms, send messages, and fetch message history. Each message has a sender, timestamp, and content.

python
from datetime import datetime

class ChatService:
    def create_room(self, room_id: str) -> None:
        ...
    def join_room(self, user_id: str, room_id: str) -> bool:
        ...
    def send_message(self, user_id: str, room_id: str,
                     content: str) -> dict:

**returns** message dict with auto-generated timestamp
        ...
    def get_history(self, room_id: str,
                    limit: int = 50, before_ts: datetime = None) -> list[dict]:
        ...
    def active_users(self, room_id: str) -> list[str]:
        ...

Example

chat = ChatService()
chat.create_room("general")
chat.join_room("alice", "general")
chat.join_room("bob",   "general")
chat.send_message("alice", "general", "Hello!")
# -> {"id": "uuid", "sender": "alice", "content": "Hello!", "ts": ...}
chat.get_history("general", limit=10)
# -> [{"sender":"alice","content":"Hello!","ts":...}]
chat.active_users("general") -> ["alice", "bob"]

Follow-ups

  1. How would you implement get_history with cursor-based pagination for efficient scrolling?
  2. How do you handle a user sending a message to a room they haven't joined?
  3. How would you push new messages to connected clients in real time (WebSockets vs. SSE vs. long polling)?
  4. How do you design the data model if rooms can have thousands of messages and you need fast lookups by time range?

Full Details

Round 1 Coding

Problem

Implement a backend for a multi-room chat service. Users can join rooms, send messages, and fetch message history. Each message has a sender, timestamp, and content.

python
from datetime import datetime

class ChatService:
    def create_room(self, room_id: str) -> None:
        ...
    def join_room(self, user_id: str, room_id: str) -> bool:
        ...
    def send_message(self, user_id: str, room_id: str,
                     content: str) -> dict:

**returns** message dict with auto-generated timestamp
        ...
    def get_history(self, room_id: str,
                    limit: int = 50, before_ts: datetime = None) -> list[dict]:
        ...
    def active_users(self, room_id: str) -> list[str]:
        ...

Example

chat = ChatService()
chat.create_room("general")
chat.join_room("alice", "general")
chat.join_room("bob",   "general")
chat.send_message("alice", "general", "Hello!")
# -> {"id": "uuid", "sender": "alice", "content": "Hello!", "ts": ...}
chat.get_history("general", limit=10)
# -> [{"sender":"alice","content":"Hello!","ts":...}]
chat.active_users("general") -> ["alice", "bob"]

Follow-ups

  1. How would you implement get_history with cursor-based pagination for efficient scrolling?
  2. How do you handle a user sending a message to a room they haven't joined?
  3. How would you push new messages to connected clients in real time (WebSockets vs. SSE vs. long polling)?
  4. How do you design the data model if rooms can have thousands of messages and you need fast lookups by time range?

About This Question

This is a candidate experience report from a notion interview during the phone round.

It covers the following topics: Coding, Sql, Phone .