
Problem
Solution
Submissions
Real-time Chat Application Backend
Certification: Advanced Level
Accuracy: 50%
Submissions: 2
Points: 20
Write a Python program to implement a backend for a real-time chat application. The backend should support user registration, authentication, private messaging, group chats, and message history. Your task is to implement the ChatServer
class with methods for user management and message handling.
Example 1
- Input: server = ChatServer() server.register_user("alice", "password123") server.register_user("bob", "secure456") alice_token = server.login("alice", "password123") bob_token = server.login("bob", "secure456") server.send_message(alice_token, "bob", "Hello Bob!") messages = server.get_messages(bob_token)
- Output: [{"from": "alice", "to": "bob", "content": "Hello Bob!", "timestamp": "2025-04-09 10:15:30"}]
- Explanation:
- Step 1: Register two users (Alice and Bob).
- Step 2: Both users log in to get authentication tokens.
- Step 3: Alice sends a private message to Bob.
- Step 4: Bob retrieves his messages, seeing Alice's message with metadata.
Example 2
- Input: server = ChatServer() server.register_user("alice", "pass123") server.register_user("bob", "pass456") server.register_user("charlie", "pass789") alice_token = server.login("alice", "pass123") bob_token = server.login("bob", "pass456") charlie_token = server.login("charlie", "pass789") group_id = server.create_group(alice_token, "Friends") server.add_to_group(alice_token, group_id, "bob") server.add_to_group(alice_token, group_id, "charlie") server.send_group_message(alice_token, group_id, "Hello everyone!") bob_groups = server.get_group_messages(bob_token, group_id) charlie_groups = server.get_group_messages(charlie_token, group_id)
- Output: [{"from": "alice", "group": "Friends", "content": "Hello everyone!", "timestamp": "2025-04-09 10:20:45"}] [{"from": "alice", "group": "Friends", "content": "Hello everyone!", "timestamp": "2025-04-09 10:20:45"}]
- Explanation:
- Step 1: Register three users and log them in.
- Step 2: Alice creates a group and adds Bob and Charlie.
- Step 3: Alice sends a message to the group.
- Step 4: Both Bob and Charlie retrieve the group message successfully.
Constraints
- 1 ≤ username length ≤ 20
- 8 ≤ password length ≤ 100
- Messages can be up to 1000 characters long
- A user can belong to a maximum of 100 groups
- Time Complexity: O(1) for sending messages, O(n) for retrieving messages where n is the number of messages
- Space Complexity: O(m+u+g) where m is the number of messages, u is the number of users, and g is the number of groups
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use JWT (JSON Web Tokens) for authentication and session management
- Store messages in an in-memory database or dictionary for quick access
- Implement proper password hashing for security
- Use timestamps to order messages chronologically
- Consider using locks or other synchronization mechanisms for concurrent access