Total Years of Work Experience From Overlapping Month Ranges
Company: LinkedIn
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Given a person's work history, compute their total work experience in whole years. Each experience has a start month and an end month, and the end month may be `"Present"`, meaning the job is ongoing. Experiences can overlap (for example, two jobs held at the same time), and time covered by more than one experience counts only once.
The report says the answer is an integer and that the overlap and `"Present"` cases must be clarified with the interviewer; this version fixes them in Rules below.
### Function Signature
```python
def total_years_of_experience(experiences: list[list[str]], current_month: str) -> int:
```
### Rules
- Each element of `experiences` is `[start, end]`. `start` is a month written `"YYYY-MM"`. `end` is either a month written `"YYYY-MM"` or the string `"Present"`, which stands for `current_month`.
- Both ends are inclusive: `["2020-01", "2020-12"]` covers 12 months and `["2020-05", "2020-05"]` covers 1 month.
- A calendar month covered by any number of experiences is counted once. Months covered by no experience are not counted.
- The answer is the number of distinct covered months divided by 12, rounded down.
- An empty `experiences` list gives 0.
### Constraints
- `0 <= len(experiences) <= 10^5`
- Every month string has a four-digit year from 1950 to 2100 and a two-digit month from 01 to 12.
- `current_month` is a valid `"YYYY-MM"` string and is never `"Present"`.
- After `"Present"` is replaced by `current_month`, every `start` is not later than its `end`, and no month is later than `current_month`.
- Experiences are in no particular order and may repeat.
### Examples
**Example 1**
```text
Input: experiences = [["2018-01", "2019-12"], ["2019-06", "2021-05"]], current_month = "2024-03"
Output: 3
```
Together the two jobs cover every month from 2018-01 to 2021-05, which is 41 months, and 41 divided by 12 rounds down to 3. Adding the two lengths separately (24 + 24 = 48 months) would count the seven overlapping months, 2019-06 to 2019-12, twice.
**Example 2**
```text
Input: experiences = [["2015-03", "2016-02"], ["2020-07", "Present"]], current_month = "2024-06"
Output: 5
```
The first job covers 12 months. The second runs from 2020-07 to 2024-06, which is 48 months. The gap between the jobs is not counted, so the total is 60 months, or 5 years.
**Example 3**
```text
Input: experiences = [["2021-02", "2021-02"], ["2020-11", "2021-04"], ["2022-01", "2022-06"]], current_month = "2023-01"
Output: 1
```
The one-month experience lies inside the second one, which covers 6 months (2020-11 to 2021-04). The third covers another 6 months, for 12 months in total, exactly 1 year.
Overview: Given a person's work history as start and end months, where an end can be Present, compute their total years of experience as an integer. It tests merging overlapping and nested month ranges, inclusive month arithmetic, resolving an open-ended end date, and rounding down to whole years.
Read the full LinkedIn Machine Learning Engineer interview experience this question came from
Given a person's work history, compute their total work experience in whole years.
Each element of `experiences` is a pair `[start, end]`. `start` is a month written `"YYYY-MM"`. `end` is either a month written `"YYYY-MM"` or the string `"Present"`, which means the job is ongoing and stands for `current_month`.
Rules:
- Both ends are inclusive: `["2020-01", "2020-12"]` covers 12 months and `["2020-05", "2020-05"]` covers 1 month.
- Experiences can overlap (for example, two jobs held at the same time). A calendar month covered by any number of experiences is counted once. Months covered by no experience are not counted.
- Return the number of distinct covered months divided by 12, rounded down.
- An empty `experiences` list gives 0.
The answer is an integer from 0 to 151 (there are only 1,812 months from 1950-01 to 2100-12), so it never exceeds 2^31 - 1 and a 32-bit `int` return is enough in every language.
### Constraints
- `0 <= len(experiences) <= 10^5`
- Every month string has a four-digit year from 1950 to 2100 and a two-digit month from 01 to 12.
- `current_month` is a valid `"YYYY-MM"` string and is never `"Present"`.
- After `"Present"` is replaced by `current_month`, every `start` is not later than its `end`, and no month is later than `current_month`.
- Experiences are in no particular order and may repeat.
### Example 1
```text
Input: experiences = [["2018-01", "2019-12"], ["2019-06", "2021-05"]], current_month = "2024-03"
Output: 3
```
Together the two jobs cover every month from 2018-01 to 2021-05, which is 41 months, and 41 divided by 12 rounds down to 3. Adding the two lengths separately (24 + 24 = 48 months) would count the seven overlapping months, 2019-06 to 2019-12, twice.
### Example 2
```text
Input: experiences = [["2015-03", "2016-02"], ["2020-07", "Present"]], current_month = "2024-06"
Output: 5
```
The first job covers 12 months. The second runs from 2020-07 to 2024-06 (`"Present"` is `current_month`), which is 48 months. The gap between the jobs is not counted, so the total is 60 months, or 5 years.
Constraints
- 0 <= len(experiences) <= 10^5
- Each experiences[i] is [start, end]: start is a "YYYY-MM" month and end is a "YYYY-MM" month or the string "Present" (meaning current_month).
- Every month string has a four-digit year from 1950 to 2100 and a two-digit month from 01 to 12.
- current_month is a valid "YYYY-MM" string and is never "Present".
- After "Present" is replaced by current_month, every start is not later than its end, and no month is later than current_month.
- Experiences are in no particular order and may repeat.
Examples
Input: ([], '2024-03')
Expected Output: 0
Explanation: Minimum valid: an empty experiences list gives 0.
Input: ([['2020-05', '2020-05']], '2024-03')
Expected Output: 0
Explanation: Minimum valid: a single one-month experience (start == end) covers 1 month; 1 // 12 = 0.
Hints
- Replace "Present" with current_month and turn each "YYYY-MM" string into a single month number, so months from different years can be compared and counted.
- Adding up each experience's length counts shared months more than once; you need the number of months in the union of all experiences.
- Divide by 12 only once, on the final month total; rounding each experience down separately loses partial years.