Lendbuzz VO 面试真题解析:按月活跃用户计费(Billing for Active Users by Month)

27次阅读
没有评论

Implement bill_for(month, plan, users) to calculate the monthly bill for a customer.

The bill depends on how many users are active during the given month. A user contributes to the bill for each day they are active in that month.

You are given:

  • month: a string like '2019-01'
  • plan: a dictionary containing the customer id and monthly price in dollars
  • users: a list of user dictionaries with activation and deactivation dates

A user is considered active on a day if they have been activated and have not yet been deactivated.

Examples:

  • When the customer has no active users during the month, the bill is 0.00.
  • When everything stays the same for a month, the bill is 8.00.
  • When a user is activated during the month, the bill is 10.84.

这道题的核心是按“月内每天活跃用户数”来累计计费:先确定目标月份的起止日期,再逐日统计哪些用户在当天处于激活状态。由于用户可能在月中激活或停用,因此不能只看整月是否存在,而要对每一天判断区间是否覆盖该日期,最后把每日费用累加得到月账单。实现时通常会利用日期遍历与区间判断,重点处理跨月边界、月初激活、月末仍未停用等情况。

正文完
 0