Prepin
Log in

Create a rate limiter for an application that employs a freemium business model.

Approach / Explanation

The inquiry is requesting that you create a system capable of regulating the frequency of specific types of requests made by users in a freemium application. This entails that your design should be able to throttle users depending on whether they are free or premium members. For instance, a free user might be limited to 5 API calls per second, whereas a premium user could have a limit of 20 API calls per second. Your system needs to accommodate varying rates for different types of requests and allow for automatic throttling of responses. Important aspects to consider when addressing this question include: Explain how you differentiate freemium users from other types of users. Illustrate how your design adjusts to varying request rates. Show how the system responds when a user surpasses their designated limit.

Suggested Answer

A rate limiter can be implemented using the Token Bucket algorithm. To begin with, I would establish a user authentication system to distinguish between premium and free users. This system would enable us to identify each user's status upon authentication and assign them the corresponding rate limit. Each user would have a token bucket associated with their account. This bucket would be filled at a constant rate, up to a specified maximum capacity. The maximum capacity and refill rates would vary between free and premium users. For example, free users might have a maximum of 100 tokens and a refill rate of 1 token per second, while premium users could have a capacity of 1000 tokens with a refill rate of 5 tokens per second. When a user makes a request, tokens equivalent to the cost of that operation would be deducted from the bucket. If the bucket does not contain enough tokens, the request would be throttled. Therefore, this design effectively distinguishes between premium and free users, accommodates different request rates, and implements a method to throttle users who exceed their allotted quota.

Alternative Answer

The approach to implementing a rate limiter can utilize Sliding Window Logs. I will start by identifying our users. Once they are authenticated, I will assign them a specific request limit based on whether they are free or premium users. For each valid request from the user, I will create a timestamp. I will maintain a record for each user that associates their ID with an array or list of the timestamps of their requests. Whenever a user makes a request, I will refer to this mapping and count all the timestamps within the allowed timeframe (for instance, the last minute). If the count of logs exceeds the set rate limit, I will throttle their request. If not, I will process their request and add the new timestamp to their logs. This method effectively distinguishes between free and premium service tiers, adapts to varying request rates, and includes a mechanism to restrict users who exceed their limits.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.