Implement a Message Queue to manage the flow of streaming requests, ensuring reliability and performance under load. The queue should accommodate multiple producers and consumers. Problem statement: Create a MessageQueue class with the following methods:
push(message: str): add a message to the queue.
pop() -> str: retrieve a message from the queue.
size() -> int: return the current queue size.
Example 1: Input: push('First message') Output: size() -> 1 Explanation: A message was added to the queue.Constraints:
1 <= message.length <= 100
Queue operations must be thread-safe.
Want to practise this one?
Create a free account to open the full question and practise Netflix questions with AI.
2. [OA] RateLimiter — control the rate of requests to streaming
Design a rate-limiting service for Netflix's API to prevent abuse and provide smooth customer experiences. Implement a class-based rate limiter that allows a specific number of requests in a given time window. Problem statement: Create a RateLimiter class with the following methods:
is_request_allowed(user_id: str, timestamp: int) -> bool: returns True if request is within the limit and False otherwise.
put_request(user_id: str, timestamp: int): logs a request.
Example 1: Input: user_id = 'user1', timestamp = 1 Output: True Explanation: User makes a request at timestamp 1.Constraints: