Chat Room System - Problem
Design a chat room system using object-oriented programming principles. The system should include three main classes:
- User: Represents a user with a name and ability to send messages
- Room: Represents a chat room that can have multiple users and stores messages
- Message: Represents a message with sender, content, and timestamp
Implement the Observer Pattern so that when a new message is sent to a room, all users in that room are automatically notified.
Your task is to implement the system and return a list of all notifications received by a specific user after a series of operations.
Input & Output
Example 1 — Basic Chat Operations
$
Input:
operations = [["createUser","Alice"],["createUser","Bob"],["createRoom","General"],["joinRoom","Alice","General"],["joinRoom","Bob","General"],["sendMessage","Alice","General","Hello everyone!"]], queryUser = "Bob"
›
Output:
["Alice in General: Hello everyone!"]
💡 Note:
Alice and Bob join General room. When Alice sends a message, Bob gets notified since he's in the same room.
Example 2 — Multiple Messages
$
Input:
operations = [["createUser","Alice"],["createUser","Bob"],["createUser","Carol"],["createRoom","General"],["joinRoom","Alice","General"],["joinRoom","Bob","General"],["joinRoom","Carol","General"],["sendMessage","Alice","General","Hi!"],["sendMessage","Bob","General","Hey Alice!"]], queryUser = "Carol"
›
Output:
["Alice in General: Hi!","Bob in General: Hey Alice!"]
💡 Note:
Carol receives notifications for both Alice's and Bob's messages since she's in the General room.
Example 3 — Multiple Rooms
$
Input:
operations = [["createUser","Alice"],["createUser","Bob"],["createRoom","General"],["createRoom","Tech"],["joinRoom","Alice","General"],["joinRoom","Alice","Tech"],["joinRoom","Bob","Tech"],["sendMessage","Bob","Tech","Technical discussion"]], queryUser = "Alice"
›
Output:
["Bob in Tech: Technical discussion"]
💡 Note:
Alice is in both rooms but only receives notification from Tech room where Bob sent the message.
Constraints
- 1 ≤ operations.length ≤ 1000
- Each operation is valid (users and rooms exist when referenced)
- User names and room names are unique strings
- Message content is non-empty string
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code