Three hard problems the way a SaaS interview frames them: a token bucket for each tenant, billing the highest plan on overlapping days, and install rounds with circular plugin groups. A 120-minute round with a plan.
The stretch round has hard problems about limits, bills and installs. The stories are about tenants, plans and plugins, but each one rests on a classic that you must name and cost. The interviewer listens for four habits.
The stretch round has three hard problems: a token bucket for each tenant, billing the highest plan on overlapping days, and install rounds with circular plugin groups. The first is a design round, where the answers come from a list of commands. Together they take a little over two hours.
This is the third of three rounds in the SaaS set. Do the core round first: stating what each structure holds carries over.
Exercise · Predict the output: A bucket that refills. A tenant's bucket holds at most 3 tokens, starts full, and refills 1 token each second. Commands: allow at second 0 for 2 tokens, allow at second 1 for 2, allow at second 2 for 2, then peek at second 3. An allow spends its tokens only if it answers 1. What does this print?
rate = 1
burst = 3
tokens = 3
last = 0
answers = []
for kind, t, cost in [("allow", 0, 2), ("allow", 1, 2), ("allow", 2, 2), ("peek", 3, 0)]:
tokens = min(burst, tokens + (t - last) * rate)
last = t
if kind == "allow":
if tokens >= cost:
tokens -= cost
answers.append(1)
else:
answers.append(0)
else:
answers.append(tokens)
print(*answers)Each problem names its move in the statement. Read the signal, then use the idea the Academy already taught you.
| Signal in the statement | The move | Academy lesson |
|---|---|---|
| a limit for each tenant, tokens refill with time, a burst size | a map from tenant to its tokens and the time of its last refill | Designing with hash maps |
| plans that overlap, only the highest rate counts on each day | sweep over the cut points with a heap of the active plans, highest rate on top | Intervals |
| plugins that need each other, install in parallel rounds | collapse each circular group, then count the longest chain of groups | Topological sort |
What is new here is the frame, not the idea. The bucket stores two numbers for each tenant and refills only when a command arrives. The sweep charges a stretch of days at one rate, so the answer is a rate times a length for each stretch. And the install problem needs groups first, because a circle of plugins has no order inside it, only an order between circles.
The bucket is a design round. State the commands and the cost of each before you code: allow, burst and peek take constant time for a tenant. The refill is where one missing word breaks the rule.
Exercise · Find the bug: A bucket with no cap. A bucket refills with elapsed time, up to its burst size. This tries to spend a cost: it returns the tokens left, or -1 when there are too few. It is right for short gaps, but wrong after a long quiet time. Which line is wrong?
def spend(tokens, last, now, cost, rate, burst):
tokens = tokens + (now - last) * rate
if tokens >= cost:
return tokens - cost
return -1A bucket never holds more than its burst: without the cap, a long quiet time saves up unlimited requests.
The install problem hides its difficulty in the groups. Count rounds only after circular plugins are merged into one group.
Exercise · Predict the output: Rounds after merging a circle. Plugin a requires plugin b for each pair [a, b]. Plugins 0 and 1 require each other, so they form one group, and they install together. A group installs one round after every group it needs. How many rounds are needed in all?
group = [0, 0, 1, 2]
requires = [[0, 1], [1, 0], [2, 0], [3, 2]]
round_of = [1, 1, 1]
for _ in range(3):
for a, b in requires:
if group[a] != group[b]:
round_of[group[a]] = max(round_of[group[a]], round_of[group[b]] + 1)
print(max(round_of))Plugins that need each other install together: collapse each circle into a group before counting rounds.
All three problems are hard, and together they make one stretch round. Each time box is close to what an interviewer allows for a hard problem. The first is a design round: you answer a list of commands, and each command has a cost.
Solve them in this order. The bucket is a map of two numbers, the billing is a sweep with a heap, and the install needs groups before rounds.
Hard problems come with a follow-up that tests whether you understood the model. Prepare one for each problem.
Treat the three problems as one long session: 120 minutes in all, with a timer, and take a short break between problems. Work out loud and name the cost before you run.
If a problem takes longer than its box, finish it anyway, but note the overrun.
Checkpoint · SaaS stretch round
Q1/5 A bucket holds at most 3 tokens, starts full and refills 1 token per second. Allows of 2 tokens come at seconds 0, 1 and 2. Which answer 1?
Answer: B
The first leaves 1 token, the second finds 2 and leaves 0, and the third finds 1 and is refused. A refusal spends nothing.
Q2/5 The same bucket is peeked at second 3, after the allows at seconds 0, 1 and 2. How many tokens does it hold?
Answer: B
At second 2 it held 1 token and the refused allow spent none. One second later it holds 2.
Q3/5 Plans run on days 0 to 3 at rate 5, days 2 to 5 at rate 8, and days 3 to 4 at rate 3. Each day is charged at the highest active rate. What is the total?
Answer: B
The days are charged 5, 5, 8, 8, 8 and 8, which is 42. Adding each plan's rate times its length gives 58 and charges overlapping days twice.
Q4/5 Plugins 0 and 1 require each other, plugin 2 requires 0, and plugin 3 requires 2. How many install rounds are needed?
Answer: A
Plugins 0 and 1 install together in round 1, plugin 2 in round 2 and plugin 3 in round 3. Counting each plugin separately gives 4.
Q5/5 Why must a bucket never hold more than its burst size?
Answer: A
The cap limits how large a burst can be. Without it, a tenant that is quiet for a long time can send a huge burst at once.
Review cards from this lesson.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.