Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Active Monthly Subscription Pricing
00:00
5 left

Active Monthly Subscription Pricing

MediumPython

Problem

Lendbuzz's subscription billing service stores plan changes as effective-month events. Implement a function that returns the active monthly price for a subscription during a requested month.

Each event either activates or changes the subscription price, or cancels the subscription. Events may be provided in any order. If multiple events have the same effective month, the event appearing later in the input takes precedence. An event affects its effective month and every later month until another event replaces it.

Formal Specification

Implement active_monthly_price(events, target_month).

  • events is a list of dictionaries with integer effective_month, nonnegative integer monthly_price in cents, and string status, which is either "active" or "cancelled".
  • target_month is an integer in YYYYMM format.
  • Return the active monthly price in cents as an integer. Return 0 if no event has taken effect yet or the latest applicable event is cancelled.

Constraints

  • 0 <= len(events) <= 10^5
  • 200001 <= effective_month, target_month <= 999912
  • All months are valid YYYYMM values
  • 0 <= monthly_price <= 10^9
  • status is either active or cancelled

Function Signature

def active_monthly_price(events, target_month):
Interviewer

Your question is Active Monthly Subscription Pricing. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.