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
INPUTAliceBobGeneral RoomOperations:1. Create users Alice, Bob2. Create General room3. Both users join room4. Alice sends message5. Query Bob's notificationsALGORITHM1Setup Observer Pattern2Users subscribe to roomMessage triggers notify()4Auto-notify all observersObserver Pattern:Room = SubjectUsers = ObserversRESULTBob's Notifications:["Alice in General:Hello everyone!"]✓ Automatic notification✓ Formatted message✓ Sender excludedKey Insight:The Observer Pattern enables automatic notifications without tight coupling.Rooms notify all subscribers instantly when messages arrive, like a smart notification system.TutorialsPoint - Chat Room System | Observer Pattern Implementation
Asked in
Meta 35 Google 28 Amazon 25 Microsoft 22 Apple 18
23.4K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen