← Questions
Thread-safe single-machine chat server with channels
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Databricks's algorithms (two rounds) round, last asked Oct 2025. It tests concurrency and object-oriented design.
Build an in-process chat system that runs on one machine. Many clients, each on its own thread, join channels, leave them, post messages, and receive the messages posted to their channels by others. It must be correct under concurrent senders and receivers, and you will be asked to explain every locking choice.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
join(user: str, channel: str) -> Noneandleave(user: str, channel: str) -> None. -
post(user: str, channel: str, text: str) -> intstores the message and returns its sequence number in that channel. Only members may post. -
receive(user: str, timeout: float) -> list[Message]returns the user's undelivered messages from all their channels, blocking up totimeoutseconds if there are none. A user does not receive their own messages. -
history(channel: str, since_seq: int) -> list[Message]returns messages after a sequence number.
Example
join("ana", "general"); join("bo", "general"); join("bo", "random")
post("ana", "general", "hi") -> 1
post("bo", "random", "lunch?") -> 1
receive("bo", timeout=0) -> [general#1 ana: "hi"]
receive("ana", timeout=0) -> [] # not in random, own message skipped
leave("bo", "general")
post("ana", "general", "bye") -> 2
receive("bo", timeout=0) -> [] # left before message 2
Constraints
- Thousands of users and channels, many threads posting and receiving at once.
- Messages in one channel are seen by everyone in the same order.
Clarifying questions
- Must every member see a channel's messages in the same order? (Yes.) Is there any ordering across channels?
- When a user joins, should they see earlier messages?
- What happens to a slow receiver: is its queue bounded, and what is dropped?
- Is history kept forever, or only the last N messages per channel?
What a strong answer covers
- Data structures: channels with a member set, a message log and a sequence counter; a per-user inbox (queue) with a condition variable for blocking receives.
- Assigning the sequence number and appending under the channel's lock, so order is the same for every member; fan-out to inboxes either under that lock or by having receivers read from channel logs with a per-user cursor.
- Lock granularity: one lock per channel and one per inbox instead of a global lock, a fixed lock order (channel before inbox) to avoid deadlock, and no blocking wait while holding a channel lock.
- Join and leave racing with a post: the membership rule (a message is delivered to members at the time it was sequenced).
- Bounded inboxes and what happens to slow consumers.
- Tests with many threads that check no message is lost, duplicated or reordered.
Follow-ups
- Add direct messages between two users.
- Make it a network server with clients on sockets. What changes?
- Persist messages so a restart loses nothing.
Where it's asked
- Databricks · Algorithms (two rounds) · Last asked Oct 202560 min · Live coding in CoderPad or similar
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
