Topic-Based News Subscription Service: Design and Implementation
Context
Design and implement an in-memory service responsible for managing news subscribers and delivering relevant news items on demand. Timestamps are integers, consistently applied across all operations (e.g., milliseconds for sliding-window rate limiting, or seconds if consistent). The service must strictly enforce the following criteria for news delivery: relevance, freshness, deduplication, specific ordering, and per-subscriber sliding-window rate limits.
API to Implement (class NewsProvider)
AddSubscription(subscriberId, minInterest, maxNewsPerSecond, topics) -> bool
Creates or updates a subscriber's configuration. Returns true on success, false on invalid input or if any constraints are violated.
RemoveSubscription(subscriberId) -> bool
Removes an existing subscriber. Returns false if the subscriberId does not exist.
NewsReceived(newsId, timestamp, interest, topics) -> bool
Ingests a new news item, including its unique ID, timestamp, an interest score, and associated topics. Returns false if newsId already exists, otherwise true.
Publish(nowTimestamp, maxAge) -> dict[int, list[int]]
At the given nowTimestamp, this method iterates through all subscribers and returns a dictionary where keys are subscriberIds and values are lists of newsIds to be delivered to that subscriber. Delivery must adhere to the following rules:
Publish Rules
- Eligibility: A news item is delivered only if its
news.interestscore is greater than or equal to thesubscriber.minInterestthreshold. - Topic Filter: A news item is delivered only if its
news.topicsset has at least one topic in common with thesubscriber.topicsset. - Freshness: A news item is delivered only if its
news.timestampfalls within the window[nowTimestamp - maxAge, nowTimestamp]. - Rate Limiting: Each subscriber can receive at most
maxNewsPerSeconditems within any rolling 1-second window. - De-duplication: A specific
newsIdmust not be delivered to the same subscriber more than once. - Ordering: News items for a subscriber must be ordered by:
- Higher
interestfirst. - If
interestscores are tied, oldertimestampfirst (ascending). - If
timestamps are also tied, smallernewsIdfirst (ascending).
- Higher
Deliverables
The solution should include:
- Specification of data structures for: managing subscriptions, storing news items, tracking delivered news per subscriber, and maintaining per-subscriber sliding-window counters for rate limiting.
- Description of algorithms for: efficient filtering, sliding-window rate limiting, de-duplication, and multi-key sorting under high throughput.
- Analysis of time and space complexity for each API operation and the
Publishmethod. - Discussion of concurrency, idempotency, and persistence considerations.
- An explanation of how the system would scale to millions of users and a large, continuous news stream, covering aspects like partitioning, sharding, backpressure mechanisms, batching strategies, and fault tolerance.
AI interview prep
Core dilemma
Designing an efficient, low-latency, in-memory system that manages complex per-subscriber state (rate limits, deduplication) and global news state, while simultaneously architecting for massive scale, high throughput, and strict real-time delivery rules across a distributed environment.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Topic-Based News Subscription Service: Design and Implementation
Context
Design and implement an in-memory service responsible for managing news subscribers and delivering relevant news items on demand. Timestamps are integers, consistently applied across all operations (e.g., milliseconds for sliding-window rate limiting, or seconds if consistent). The service must strictly enforce the following criteria for news delivery: relevance, freshness, deduplication, specific ordering, and per-subscriber sliding-window rate limits.
API to Implement (class NewsProvider)
AddSubscription(subscriberId, minInterest, maxNewsPerSecond, topics) -> bool
Creates or updates a subscriber's configuration. Returns true on success, false on invalid input or if any constraints are violated.
RemoveSubscription(subscriberId) -> bool
Removes an existing subscriber. Returns false if the subscriberId does not exist.
NewsReceived(newsId, timestamp, interest, topics) -> bool
Ingests a new news item, including its unique ID, timestamp, an interest score, and associated topics. Returns false if newsId already exists, otherwise true.
Publish(nowTimestamp, maxAge) -> dict[int, list[int]]
At the given nowTimestamp, this method iterates through all subscribers and returns a dictionary where keys are subscriberIds and values are lists of newsIds to be delivered to that subscriber. Delivery must adhere to the following rules:
Publish Rules
- Eligibility: A news item is delivered only if its
news.interestscore is greater than or equal to thesubscriber.minInterestthreshold. - Topic Filter: A news item is delivered only if its
news.topicsset has at least one topic in common with thesubscriber.topicsset. - Freshness: A news item is delivered only if its
news.timestampfalls within the window[nowTimestamp - maxAge, nowTimestamp]. - Rate Limiting: Each subscriber can receive at most
maxNewsPerSeconditems within any rolling 1-second window. - De-duplication: A specific
newsIdmust not be delivered to the same subscriber more than once. - Ordering: News items for a subscriber must be ordered by:
- Higher
interestfirst. - If
interestscores are tied, oldertimestampfirst (ascending). - If
timestamps are also tied, smallernewsIdfirst (ascending).
- Higher
Deliverables
The solution should include:
- Specification of data structures for: managing subscriptions, storing news items, tracking delivered news per subscriber, and maintaining per-subscriber sliding-window counters for rate limiting.
- Description of algorithms for: efficient filtering, sliding-window rate limiting, de-duplication, and multi-key sorting under high throughput.
- Analysis of time and space complexity for each API operation and the
Publishmethod. - Discussion of concurrency, idempotency, and persistence considerations.
- An explanation of how the system would scale to millions of users and a large, continuous news stream, covering aspects like partitioning, sharding, backpressure mechanisms, batching strategies, and fault tolerance.