Practice problem · Snowflake · Medium

Tiered Rate Limiter

An API has clients on different plans: free, pro, enterprise. 3 stages in Python with an AI assistant. Pro.

Stage 1 of 3: Buckets by tier

Background. An API has clients on different plans: free, pro, enterprise. Each plan allows a certain request rate, and a request that goes over its client's rate is refused. The service that makes that call is a rate limiter.

Implement RateLimiter in limiter.py.

  • RateLimiter(tiers, default_policy, clock=time.monotonic). tiers maps a tier name to a TierLimit(capacity, refill_per_second). default_policy is "allow" or "deny" (anything else raises ValueError). clock is a callable returning the current time in seconds; the tests pass their own.
  • set_client_tier(client_id, tier) records which tier a client belongs to.
  • allow(client_id) returns True when the request is allowed and False when it is refused.

Each client has its own token bucket. The bucket is full (capacity tokens) the first time the client is seen. Tokens accrue continuously at refill_per_second and never exceed capacity; a fraction of a token is kept. A request is allowed when the bucket holds at least one token, and takes one. A refused request takes nothing.

A client with no tier, or whose tier is not in tiers, follows the default policy: "allow" admits every request, "deny" refuses every request.

Example

clock = FakeClock()                      # test_helpers.py: clock() returns clock.now
rl = RateLimiter({"free": TierLimit(2, 1.0)}, "deny", clock=clock)
rl.set_client_tier("ana", "free")
[rl.allow("ana") for _ in range(3)]      # [True, True, False]
clock.advance(0.5)
rl.allow("ana")                          # False: half a token
clock.advance(0.5)
rl.allow("ana")                          # True
rl.allow("guest")                        # False: no tier, default is deny

Done when Submit goes green.

Constraints

  • Python 3.12+ standard library only
  • Time limit: 10 seconds per test file
  • Editable: limiter.py
  • Read-only: test_helpers.py and all tests/test_*.py files

What comes after stage 1

The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.

  1. Stage 2: Limits change at runtime. Tiers are edited while clients are mid-burst; the change must reach them without a restart.
  2. Stage 3: One downstream, many tiers. Everything admitted goes to one service with a hard ceiling; enterprise should feel it last, and a refusal says when to retry.

The files you get

  • Yours to edit: limiter.py.
  • Read-only: test_helpers.py, tests/__init__.py.
  • Tests you can read and run: tests/test_part1_buckets.py. Hidden tests run on Submit.

The round: an agent in the editor

The assistant here works like the one in the interview IDE. It reads your files, runs code and proposes changes, and you accept or reject each one. It doesn't volunteer ideas, so what gets built is what you asked for. Along the way the interviewer asks about your decisions, and at the end the report says what you checked and what you accepted unread.

Frequently asked, briefly answered

Is Tiered Rate Limiter free?

It is a Pro problem. Free plan includes one complete problem, plus the first stage of several more, so you can see how a round works before you pay.

Which company asks something like this?

Snowflake. It is modelled on Snowflake's round: Technical phone screen in CoderPad (60 minutes, Python) with AI allowed, graded on how you gather requirements, test and debug; the AI Engineer onsite adds an in-person "AI Augmented coding" round on your own laptop with any assistant, where you explain the algorithm, the alternatives and how you reviewed the generated code.

Do I bring my own AI tool?

No. The assistant is in the editor, same as in the interview IDEs at Meta, Stripe and Amazon. What you choose at Begin is the format: an agent that proposes changes you accept or reject, an answer-only assistant that can't write code, or no assistant at all.

What does the interviewer ask?

2 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.

Start the problem

Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the Pro plan starts at the first stage of this problem.

Start this problem → Sign in

More like it: every practice problem, how Snowflake runs its AI round, the AI coding interview guide.