← 返回 optiver 的题目列表Design News Push System
类型:online_judge
Design a news push system. The system should implement the following functions:
AddSubscription(user_id, topics): User subscribes to certain news topics.
RemoveSubscription(user_id, topics): User unsubscribes from certain news topics.
NewsReceived(news_id, topics): The system receives a news item with its associated topics.
Publish(): Push relevant news to each user based on their subscribed topics.
Here are the parameters and return types for each function:
AddSubscription(user_id: str, topics: List[str]) -> None
RemoveSubscription(user_id: str, topics: List[str]) -> None
NewsReceived(news_id: str, topics: List[str]) -> None
Publish() -> Dict[str, List[str]]: Returns a dictionary where the key is user ID, and the value is a list of news IDs that should be pushed to the user.
Provide an implementation of this system, assuming the following test cases:
Test Case 1:
AddSubscription("user1", ["sports", "technology"])
NewsReceived("news1", ["sports"])
NewsReceived("news2", ["politics"])
Publish()
Expected Output:
{"user1": ["news1"]}
Data scale limitations: The number of users and news items will not exceed 1000.
Example
Input
AddSubscription("user1", ["sports", "technology"])
NewsReceived("news1", ["sports"])
NewsReceived("news2", ["politics"])
Publish()