Karat VO 面试真题解析:按年份统计收入计算器(Income Calculator by Year)

20次阅读
没有评论

Task:

  • Implement the following problem
  • Write a unit test for the implementation

We want to implement an income calculator that, given a compensation package and a start date, returns the income earned in each year up to the current date.

The input is:

  • Start date (SimpleDate)
  • Base salary per year (int)
  • RSU per year (int)
  • Sign on bonus, only once (int)

SimpleDate is my own implementation of Date. It has these fields:

  • SimpleDate:
  • month: int
  • year: int

You can also use SimpleDate.now() to get the current date.

Example: Given these values

  • Base salary: 120,000
  • Annual RSU: 60,000
  • One-time Sign on: 25,000
  • Start date: 02/2021
  • Current date: 02/2023

The calculator should return:

  • 2021: 190,000 (11 months income, including February plus sign on)
  • 2022: 180,000 (12 months income)
  • 2023: 15,000 (only January, excluding February)

这道题要求实现一个按年份拆分的收入计算器:给定入职月份、年 base salary、每年 RSU 以及一次性 sign-on bonus,统计从开始日期到当前日期之间每一年的总收入。核心做法是按年份和月份区间逐段计算,先处理起始年的剩余月份、再处理中间完整年份、最后处理当前年的已过去月份;其中 sign-on bonus 只在开始年份计入一次。实现时要特别注意月份边界,例如示例中从 02/2021 到 02/2023,2021 年只算入职后的 11 个月并加上签约奖金,2022 年按完整 12 个月计算,2023 年只算 1 月。

正文完
 0