Three easy problems the way a SaaS interview frames them: users who return the next day, the peak seats of a workspace, and subscriptions that expired. A 45-minute round with a plan.
A software-as-a-service company sells a product by subscription to teams. Its coding interviews use the same data structures as any other company, but the stories are about workspaces, seats, subscriptions and activity logs. The interviewer listens for four habits.
The warm-up round tests these habits on three easy problems. The interviewer often opens with "What would you ask?" Have your questions ready.
Is the event log in time order? Can the same event arrive twice? Is the grace period part of the expiry? Does a day with no events still need an answer?
This is the first of three rounds in the SaaS set. The core and stretch rounds that follow use the same habits on harder problems. They cover usage quotas, permissions in a folder tree, rate limits for each tenant and plans that overlap.
Exercise · Predict the output: The peak and the final count. One workspace logs events as a user and an action: 1 joins, 0 leaves. A join by a member, or a leave by a non-member, changes nothing. What does this print: the peak number of seats used, then the seats in use at the end?
users = [1, 2, 1, 3, 2]
actions = [1, 1, 1, 0, 0]
members = set()
peak = 0
for user, action in zip(users, actions):
if action == 1:
members.add(user)
else:
members.discard(user)
peak = max(peak, len(members))
print(peak, len(members))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 |
|---|---|---|
| users active on a day and the day before | a set of users for each day, and one answer for every day | Seen-set and complement |
| the peak seats for each workspace, events in order | a set of members for each workspace, with the peak updated after every event | Counting and grouping |
| how many subscriptions expired before a cutoff, in a sorted list | a binary search for the first expiry not before the cutoff | Lower and upper bound |
What is new here is the frame, not the idea. The key has two fields, the workspace and the user, and a set makes every event safe to repeat. The returning users are an intersection of two days, so a quiet day in between gives zero. And the grace period moves the cutoff: a subscription is expired only when its expiry plus the grace is before the day.
The returning-user report is the one with a hidden shape. It must have an entry for every day, even days with no events.
Exercise · Predict the output: Users who return the next day. Each event is a day and a user. A user is returning on a day when they were active that day and the day before. Day 0 has no day before it. What does this print: the returning users for days 0 to 4?
days = [0, 0, 1, 1, 3, 3, 4]
users = [5, 6, 5, 7, 5, 7, 5]
active = {}
for day, user in zip(days, users):
active.setdefault(day, set()).add(user)
result = []
for day in range(5):
if day == 0:
result.append(0)
else:
result.append(len(active.get(day, set()) & active.get(day - 1, set())))
print(*result)The expiry problem is a binary search with one delicate comparison. Expired means strictly before the day, once the grace is added.
Exercise · Find the bug: Expired on the day itself. The expiry days are sorted. A subscription with expiry e is expired on day d when e plus the grace is before d. This returns how many are expired on a day. It is right except when an expiry plus the grace equals the day. Which line is wrong?
def first_at_least(values, x):
lo, hi = 0, len(values)
while lo < hi:
mid = (lo + hi) // 2
if values[mid] < x:
lo = mid + 1
else:
hi = mid
return lo
def count_expired(expiries, day, grace):
return first_at_least(expiries, day - grace + 1)Expired means strictly before the day: search for the cutoff itself, not the cutoff plus one.
A quiet day breaks the streak and still needs an answer: build one entry for every day.
All three problems are easy, and together they make one warm-up round. Each time box is close to what an interviewer allows for an easy problem, with time to explain.
Solve them in this order. The returning users are sets for each day, the seats add events that may repeat, and the expiries add a binary search.
A warm-up problem rarely ends at the first answer. Once your code works, the interviewer changes one rule and asks what changes in your code. Prepare one follow-up for each problem.
Treat the three problems as one interview: 45 minutes in all, with a timer. Work out loud, as if someone is listening.
If a problem takes longer than its box, finish it anyway, but note the overrun. In a real round the interviewer would have moved on, so the next attempt should be faster.
Checkpoint · SaaS warm-up round
Q1/5 The same user id joins workspace 1 and workspace 2. How many seats are used in total?
Answer: B
The workspace is part of the key, so the user holds one seat in each workspace. Billing is per workspace.
Q2/5 A user who is already a member joins again. What should the seat count do?
Answer: B
The event is a repeat, so it must change nothing. A set makes that automatic.
Q3/5 Events leave 1 seat at the end, but 2 seats were used at the busiest moment. What does billing charge for?
Answer: A
Finance charges for the high-water mark, the most seats in use at once. The final count is not the charge.
Q4/5 A user was active on days 3 and 5 only. Is the user returning on day 5?
Answer: B
Day 4 was quiet for this user, so the streak is broken. Returning needs activity on the day before.
Q5/5 Expiries are 3, 5 and 8, the grace is 0 and the day is 5. How many subscriptions are expired?
Answer: A
Only the expiry 3 is strictly before day 5. The expiry 5 is still valid on day 5.
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.