Preparing your design workspace

Loading the problem and your saved attempt…

Design and Implement a Simple Web Chat

Model and code a small in-memory chat: users, named rooms, membership rules, numbered room messages, and history pages that stay correct while new messages arrive.

Design and implement the core of a simple web chat where people talk in named chat rooms. For this exercise, assume the following rules.

Users are registered once with a unique user ID and a display name. The web page passes the user ID in with every request, so signing in is not part of your design. Any registered user can create a room with a name no other room uses, and the creator is a member of the new room.

A user can join any room and can leave a room they are in. Only a member of a room can send messages to it or read its history, and a member can read the whole history, including messages sent before they joined.

A message is plain text that is not empty or only spaces and is at most 500 characters long. When a room accepts a message, it stores who sent it, the text, and the time it was accepted, and gives it a number in that room: 1 for the room's first message, then 2, 3, and so on, with no gaps.

That number is the room's order. Two messages can be accepted at the same clock time, so time never decides the order. Tests must be able to control the time a message records.

A member reads history one page at a time and chooses a page size from 1 to 50. The first page holds the room's newest messages, each later page holds the next older ones, and each page lists its messages oldest first.

New messages can arrive while someone is paging back, and paging must still never skip or repeat a message.

A request that breaks a rule is refused with a clear reason and changes nothing.

That covers an unknown user or room, a user ID or room name that is already taken, sending to or reading a room you are not a member of, leaving a room you are not in, empty or too-long text, a page size outside 1 to 50, and a page position the chat never handed out. Messages are never edited or deleted.

Design the objects, their responsibilities, the method contracts, and the order of work for sending and reading, then implement the core in a mainstream language and show how you would test it. Everything is in memory, in one process, on one thread.

Real network code, websockets, login, databases, file or image messages, encryption, direct messages, typing indicators, and read receipts are out of scope.