AlgoMaster Logo
AlgoMasterDesign Chat Roomeasy

Design Chat Room

easy

A chat room sits between its users. A user hands a message to the room, and the room decides who receives it. No user ever holds a reference to another user, not even to whisper.

ChatRoomFacade is provided in the starter code. Implement the types it uses: the ChatMediator contract, the User colleague, and the ChatRoom concrete mediator (IChatMediator in C#).

  • ChatMediator exposes broadcast(sender, text), which returns how many members received the message, and whisper(sender, to, text), which returns whether it was delivered.
  • User(name, mediator) exposes getName(), send(text) and sendPrivate(to, text), which delegate to the mediator, plus receive(line) and inbox().
  • ChatRoom exposes join(user), leave(user) and memberCount(), and implements the contract. broadcast delivers "<sender>: <text>" to every member except the sender. whisper delivers "<sender> (private): <text>" to the member named to and returns true; a name that belongs to nobody else in the room, including the sender's own, returns false.

The provided ChatRoomFacade exposes this API:

  • ChatRoomFacade() creates an empty room.
  • boolean join(String name) creates a user with that name, joins the room, and returns true. An empty name or a name already in the room returns false.
  • boolean leave(String name) removes that user and returns true, or false for a name not in the room.
  • int send(String name, String text) broadcasts from that user and returns the delivery count, or -1 for a name not in the room.
  • boolean whisper(String from, String to, String text) sends a private message and returns whether it was delivered. An unknown from returns false.
  • String[] inbox(String name) returns the lines that user has received, oldest first, or an empty array for a name not in the room.
  • int memberCount() returns how many users are in the room.

A user who leaves takes their inbox with them, so joining again under the same name starts with an empty inbox.

The tests call the provided ChatRoomFacade; your work should be confined to the mediator contract, the user, and the room.

Example 1:

Input:

Output:

Explanation: Alice's message reaches the one other member. The room does not echo it back to Alice, so her own inbox stays empty.

Example 2:

Input:

Output:

Explanation: A whisper goes to exactly one named member. A broadcast from the third member then reaches the other two.

Constraints

  • 0 <= name.length <= 20
  • 1 <= text.length <= 60
  • At most 100 calls in total are made across all methods.

Starter Code

Implement the ChatMediator contract, the User colleague, and the ChatRoom mediator. ChatRoomFacade is complete and must not be modified.

How the design is graded

needs 7/10 to pass
  • Colleagues know only the mediator

    Full marks when `User` holds a `ChatMediator` and nothing else about other users, and both `send` and `sendPrivate` delegate to it. Lose points heavily when a user holds a list of users, looks another user up itself, or formats a line for someone else's inbox.

  • The room decides who receives

    Full marks when `ChatRoom.broadcast` skips the sender by identity and delivers `<sender>: <text>` to everyone else, and `whisper` finds the named member, refuses the sender's own name and an unknown name, and delivers `<sender> (private): <text>`. Lose points when the sender receives their own message or a whisper reaches more than one member.

  • Membership is by object

    Full marks when `join` refuses a duplicate object, `leave` removes that same object, and a removed user neither sends nor receives. Lose points for printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new ChatRoomFacade()null
join("alice")true
join("bob")true
send("alice", "hi")1
inbox("bob")["alice: hi"]
inbox("alice")[]

Alice's message reaches the one other member. The room does not echo it back to Alice, so her own inbox stays empty.

Run checks these cases. Submit also runs a larger hidden set.