Design a subscription planner with recurring billing on a virtual day clock

Quick Overview

Design a subscription planner that registers plans, subscribes and cancels users, advances a virtual day clock, and bills every recurring charge that falls due. It tests choosing data structures for scheduled events, keeping cancelled subscriptions from billing again, and reasoning about the cost of large jumps in time.

Design a subscription planner with recurring billing on a virtual day clock

Company: Figma

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a subscription planner that manages user subscriptions to plans, processes recurring billing on a virtual timeline, and reports per-user revenue. The system maintains a current virtual time measured in days, starting at day `0`. Plans have a fixed price and a billing cycle in days. When a user subscribes to a plan at day `t`, they are charged immediately and the next charge is scheduled `t + cycle` days later. Calling `advanceTime` moves the clock forward and processes all due charges in chronological order. Ties are broken by ascending `userId`, then ascending `planId`. Cancelling a subscription stops future charges for that user and plan pair but does not refund past charges. Implement the following methods: - `addPlan(planId: int, price: int, cycleDays: int)`: Register a plan with the given price and billing cycle in days. If the planId already exists, overwrite its price and cycle. - `subscribe(userId: int, planId: int) -> bool`: Subscribe the user to the plan at the current time, charging them immediately and scheduling the next charge. Returns false if the plan does not exist or the user is already subscribed to it; otherwise true. - `cancel(userId: int, planId: int) -> bool`: Cancel the user's subscription to the given plan, stopping any future charges. Returns true if an active subscription was cancelled, false otherwise. - `advanceTime(days: int)`: Advance the virtual clock by the given number of days, processing all due recurring charges in chronological order (ties by userId, then planId). - `getTotalSpent(userId: int) -> int`: Return the total amount the user has been charged across all subscriptions so far. Example 1, two users on the same plan: ```text Input: ["SubscriptionPlanner", "addPlan", "subscribe", "subscribe", "advanceTime", "getTotalSpent", "getTotalSpent", "cancel"] [[], [5, 100, 10], [1, 5], [2, 5], [25], [1], [2], [3, 5]] Output: [null, null, true, true, null, 300, 300, false] Explanation: planner = SubscriptionPlanner() # current day = 0 planner.addPlan(5, 100, 10) # plan 5: $100 every 10 days planner.subscribe(1, 5) # True, user 1 charged $100 planner.subscribe(2, 5) # True, user 2 charged $100 planner.advanceTime(25) # charges at days 10 and 20 for both users planner.getTotalSpent(1) # 300 = $100 x 3 planner.getTotalSpent(2) # 300 = $100 x 3 planner.cancel(3, 5) # False, user 3 was never subscribed ``` Example 2, cancelling and subscribing again: ```text Input: ["SubscriptionPlanner", "addPlan", "subscribe", "advanceTime", "cancel", "subscribe", "advanceTime", "getTotalSpent", "subscribe"] [[], [1, 50, 10], [7, 1], [5], [7, 1], [7, 1], [10], [7], [7, 2]] Output: [null, null, true, null, true, true, null, 150, false] Explanation: planner = SubscriptionPlanner() # current day = 0 planner.addPlan(1, 50, 10) # plan 1: $50 every 10 days planner.subscribe(7, 1) # True, charged $50, next charge due day 10 planner.advanceTime(5) # day 5, nothing due planner.cancel(7, 1) # True, the day-10 charge will not happen planner.subscribe(7, 1) # True, new subscription: charged $50, next charge due day 15 planner.advanceTime(10) # day 15, one charge on day 15 planner.getTotalSpent(7) # 150 = $50 x 3 planner.subscribe(7, 2) # False, plan 2 does not exist ``` The cancelled subscription's day-10 charge never happens, and the new subscription is billed on its own schedule (days 5 and 15), so user 7 has paid three times. ```hint Ordering due charges You repeatedly need the earliest due charge while new charges keep being scheduled. Pick a structure that returns the minimum cheaply, and check whether its ordering can encode the userId and planId tie-break for free. ``` ```hint Cancel, then subscribe again Deleting from the middle of a priority queue is awkward, so skipping cancelled entries when they come up later is tempting. Trace what happens to the old scheduled entry when a user cancels and subscribes to the same plan again before that entry's day. ``` ### Constraints & Assumptions - `1 <= planId, userId <= 10^4` - `1 <= price <= 10^6` - `1 <= cycleDays <= 365` - `1 <= days <= 10^5` - At most `10^4` total operations. - Virtual time starts at day `0` and only moves forward through `advanceTime`. - A charge due exactly on the day `advanceTime` reaches is processed in that call (Example 2). ### Clarifying Questions to Ask - When `addPlan` overwrites a plan that already has subscribers, do they move to the new price and cycle, and starting from which charge? - If a user cancels and subscribes again to the same plan, is that a new subscription with its own immediate charge and schedule? - Does anything besides `getTotalSpent` depend on the order of charges, such as receipts or a charge log, or are totals the only output? - Can a plan ever be removed, or only added and overwritten? ### What a Strong Answer Covers - The data structures for plans, live subscriptions, per-user totals and scheduled charges, with the cost of each operation. - How `advanceTime` finds due charges in day, userId, planId order and schedules each next charge from the previous charge day. - How cancellation stops future charges, including a user who cancels and subscribes again before the old charge date. - A stated rule for what an overwritten plan means for existing subscribers. - The cost of `advanceTime` when one call covers many billing cycles, and a way to bound it. ### Follow-up Questions - With a 1-day plan, 5,000 subscribers and `advanceTime(100000)`, how many heap operations does your design do, and how would you reduce that? - How would you let existing subscribers keep their old price and cycle after `addPlan` changes the plan? - How would you produce an ordered log of every charge (day, user, plan, amount) while still handling long time jumps quickly? - How would the design change if a charge could fail and had to be retried before the subscription lapses?

Overview: Design a subscription planner that registers plans, subscribes and cancels users, advances a virtual day clock, and bills every recurring charge that falls due. It tests choosing data structures for scheduled events, keeping cancelled subscriptions from billing again, and reasoning about the cost of large jumps in time.

Figma logo
Figma
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a subscription planner that manages user subscriptions to plans, processes recurring billing on a virtual timeline, and reports per-user revenue.

The system maintains a current virtual time measured in days, starting at day 0. Plans have a fixed price and a billing cycle in days. When a user subscribes to a plan at day t, they are charged immediately and the next charge is scheduled t + cycle days later.

Calling advanceTime moves the clock forward and processes all due charges in chronological order. Ties are broken by ascending userId, then ascending planId. Cancelling a subscription stops future charges for that user and plan pair but does not refund past charges.

Implement the following methods:

  • addPlan(planId: int, price: int, cycleDays: int) : Register a plan with the given price and billing cycle in days. If the planId already exists, overwrite its price and cycle.
  • subscribe(userId: int, planId: int) -> bool : Subscribe the user to the plan at the current time, charging them immediately and scheduling the next charge. Returns false if the plan does not exist or the user is already subscribed to it; otherwise true.
  • cancel(userId: int, planId: int) -> bool : Cancel the user's subscription to the given plan, stopping any future charges. Returns true if an active subscription was cancelled, false otherwise.
  • advanceTime(days: int) : Advance the virtual clock by the given number of days, processing all due recurring charges in chronological order (ties by userId, then planId).
  • getTotalSpent(userId: int) -> int : Return the total amount the user has been charged across all subscriptions so far.

Example 1, two users on the same plan:

Input:
["SubscriptionPlanner", "addPlan",    "subscribe", "subscribe", "advanceTime", "getTotalSpent", "getTotalSpent", "cancel"]
[[],                    [5, 100, 10], [1, 5],      [2, 5],      [25],          [1],             [2],             [3, 5]]

Output: [null, null, true, true, null, 300, 300, false]

Explanation:
planner = SubscriptionPlanner()         # current day = 0
planner.addPlan(5, 100, 10)             # plan 5: $100 every 10 days
planner.subscribe(1, 5)                 # True, user 1 charged $100
planner.subscribe(2, 5)                 # True, user 2 charged $100
planner.advanceTime(25)                 # charges at days 10 and 20 for both users
planner.getTotalSpent(1)                # 300 = $100 x 3
planner.getTotalSpent(2)                # 300 = $100 x 3
planner.cancel(3, 5)                    # False, user 3 was never subscribed

Example 2, cancelling and subscribing again:

Input:
["SubscriptionPlanner", "addPlan",   "subscribe", "advanceTime", "cancel", "subscribe", "advanceTime", "getTotalSpent", "subscribe"]
[[],                    [1, 50, 10], [7, 1],      [5],           [7, 1],   [7, 1],      [10],          [7],             [7, 2]]

Output: [null, null, true, null, true, true, null, 150, false]

Explanation:
planner = SubscriptionPlanner()         # current day = 0
planner.addPlan(1, 50, 10)              # plan 1: $50 every 10 days
planner.subscribe(7, 1)                 # True, charged $50, next charge due day 10
planner.advanceTime(5)                  # day 5, nothing due
planner.cancel(7, 1)                    # True, the day-10 charge will not happen
planner.subscribe(7, 1)                 # True, new subscription: charged $50, next charge due day 15
planner.advanceTime(10)                 # day 15, one charge on day 15
planner.getTotalSpent(7)                # 150 = $50 x 3
planner.subscribe(7, 2)                 # False, plan 2 does not exist

The cancelled subscription's day-10 charge never happens, and the new subscription is billed on its own schedule (days 5 and 15), so user 7 has paid three times.

Constraints & Assumptions

  • 1 <= planId, userId <= 10^4
  • 1 <= price <= 10^6
  • 1 <= cycleDays <= 365
  • 1 <= days <= 10^5
  • At most 10^4 total operations.
  • Virtual time starts at day 0 and only moves forward through advanceTime .
  • A charge due exactly on the day advanceTime reaches is processed in that call (Example 2).

Clarifying Questions to Ask Guidance

  • When addPlan overwrites a plan that already has subscribers, do they move to the new price and cycle, and starting from which charge?
  • If a user cancels and subscribes again to the same plan, is that a new subscription with its own immediate charge and schedule?
  • Does anything besides getTotalSpent depend on the order of charges, such as receipts or a charge log, or are totals the only output?
  • Can a plan ever be removed, or only added and overwritten?

What a Strong Answer Covers Guidance

  • The data structures for plans, live subscriptions, per-user totals and scheduled charges, with the cost of each operation.
  • How advanceTime finds due charges in day, userId, planId order and schedules each next charge from the previous charge day.
  • How cancellation stops future charges, including a user who cancels and subscribes again before the old charge date.
  • A stated rule for what an overwritten plan means for existing subscribers.
  • The cost of advanceTime when one call covers many billing cycles, and a way to bound it.

Follow-up Questions Guidance

  • With a 1-day plan, 5,000 subscribers and advanceTime(100000) , how many heap operations does your design do, and how would you reduce that?
  • How would you let existing subscribers keep their old price and cycle after addPlan changes the plan?
  • How would you produce an ordered log of every charge (day, user, plan, amount) while still handling long time jumps quickly?
  • How would the design change if a charge could fail and had to be retried before the subscription lapses?