Build a multi-room chat HTTP service
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Notion's practical coding and debugging round, last asked May 2022. It tests http api, python and messaging.
In about an hour, build a small chat backend that speaks JSON over HTTP. Users join rooms and post messages. Each user can fetch a room's new messages since they last looked, so they only see what is new to them. Write tests for the endpoints. Any Python web framework is fine, for example FastAPI with pytest.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
POST /rooms/{room}/joinwith{"user": "ana"}— adds the user to the room (creating the room if new). Joining twice is fine. Returns200. -
POST /rooms/{room}/messageswith{"user": "ana", "text": "hi"}— appends a message. Returns201with{"id": 1, "user": "ana", "text": "hi"}. Ids increase per room. A user who hasn't joined gets403. -
GET /rooms/{room}/messages?user=ana— returns{"messages": [...]}: the messages posted after this user's last fetch (all messages since they joined, the first time), oldest first, and moves the user's read position to the newest one. - Unknown room:
404. Missing or bad fields:400or422.
Example
POST /rooms/general/join {"user": "ana"} -> 200
POST /rooms/general/join {"user": "bo"} -> 200
POST /rooms/general/messages {"user": "ana", "text": "hi"} -> 201 {"id": 1, ...}
POST /rooms/general/messages {"user": "bo", "text": "hey"} -> 201 {"id": 2, ...}
GET /rooms/general/messages?user=bo -> ids [1, 2]
POST /rooms/general/messages {"user": "ana", "text": "lunch?"} -> 201 {"id": 3, ...}
GET /rooms/general/messages?user=bo -> ids [3]
GET /rooms/general/messages?user=bo -> []
GET /rooms/general/messages?user=ana -> ids [1, 2, 3]
Constraints
- State in memory is fine. Handle concurrent requests safely.
Clarifying questions
- Does a new member see messages posted before they joined?
- Should fetching move the read position automatically, or should the client acknowledge what it saw?
- Do users post to rooms they haven't joined?
- Is there a page size limit for history?
- How is the user identified: a field, a header, or a token?
What a strong answer covers
- Data model: rooms with an ordered message list and a next id, members per room, and each member's last-read id per room.
- Fetch as "messages with id greater than last-read", using the ids as a cursor rather than list positions, then updating last-read.
- Clear status codes and validation, and a lock (or an async-safe design) around shared state.
- Tests through the framework's test client covering joining, posting, two users reading at different times, unknown rooms and non-members.
Follow-ups
- Add
limitand abeforecursor to page through older history. - Push new messages to connected clients (long polling, server-sent events or WebSockets) instead of polling.
- Store messages in a database and run several server instances. What must change about ids and read positions?
Where it's asked
- Notion · Practical coding and debugging · Last asked May 202260 min · Coding or debugging inside a realistic codebase
What it tests
http api · python · messaging
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.
