Define and Implement Natural-Month Date Comparison
Quick Overview
Define and implement a calendar-based comparison that classifies two dates as within, exactly, or over one natural month. Apply absolute ordering, month-end clamping, leap-year behavior, and cross-year boundaries consistently with a date library.
Define and Implement Natural-Month Date Comparison
Company: Apple
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Onsite
## Question
Define and implement a function that classifies two calendar dates as `within_one_month`, `exactly_one_month`, or `over_one_month`. The phrase “one month” is intentionally ambiguous: establish a simple calendar-based rule before writing code, then apply it consistently across month and year boundaries.
### Constraints & Assumptions
- Inputs are valid date-only values with no time zone or time of day.
- Compare absolute distance: order the two dates first.
- Use this rule: add one calendar month to the earlier date; if the target month lacks that day, clamp to its last valid day.
- Compare the later date with that one-month boundary.
- Use the platform's date library rather than hand-coding month lengths when available.
### Clarifying Questions to Ask
- Is a month a fixed number of days? No, use the stated calendar-month rule.
- How is January 31 plus one month handled? Clamp to the last valid day of February.
- Does argument order matter? No, classify absolute distance.
- Are time zones or timestamps in scope? No.
```hint Make the definition executable
Compute one explicit boundary date from the earlier input, then ordinary date comparison yields all three classes.
```
### What a Strong Answer Covers
- Recognition that requirements must define month-end behavior.
- Correct date ordering, month addition, year rollover, and clamping.
- Use of a reliable date API with tests around short months.
- A compact implementation whose branches directly match the three outputs.
- Awareness that a different business definition would require different tests, not a silent assumption.
### Follow-up Questions
1. How would timestamps and user time zones change the contract?
2. What if the business defines one month as thirty elapsed days?
3. Which property-based tests would you add around month ends?
Quick Answer: Define and implement a calendar-based comparison that classifies two dates as within, exactly, or over one natural month. Apply absolute ordering, month-end clamping, leap-year behavior, and cross-year boundaries consistently with a date library.
Define and implement a function that classifies two calendar dates as within_one_month, exactly_one_month, or over_one_month. The phrase “one month” is intentionally ambiguous: establish a simple calendar-based rule before writing code, then apply it consistently across month and year boundaries.
Constraints & Assumptions
Inputs are valid date-only values with no time zone or time of day.
Compare absolute distance: order the two dates first.
Use this rule: add one calendar month to the earlier date; if the target month lacks that day, clamp to its last valid day.
Compare the later date with that one-month boundary.
Use the platform's date library rather than hand-coding month lengths when available.
Clarifying Questions to Ask Guidance
Is a month a fixed number of days? No, use the stated calendar-month rule.
How is January 31 plus one month handled? Clamp to the last valid day of February.
Does argument order matter? No, classify absolute distance.
Are time zones or timestamps in scope? No.
What a Strong Answer Covers Guidance
Recognition that requirements must define month-end behavior.
Correct date ordering, month addition, year rollover, and clamping.
Use of a reliable date API with tests around short months.
A compact implementation whose branches directly match the three outputs.
Awareness that a different business definition would require different tests, not a silent assumption.
Follow-up Questions Guidance
How would timestamps and user time zones change the contract?
What if the business defines one month as thirty elapsed days?
Which property-based tests would you add around month ends?