The constraint is what makes this annoying.
First, clarify that 'today' can be obtained via a simple system call or assume a given current date, then implement date arithmetic from scratch. Convert both dates to a day count relative to a fixed epoch (e.g., days since 0000-01-01) using leap year rules, and return the difference. Handle edge cases like invalid dates and future dates.
Pro tip: Mention that you would validate the input date and discuss the trade-off between assuming a fixed 'today' for testability versus using the actual current date. This shows attention to correctness and testability, which Amazon values.
Confirm the definition of 'today' (e.g., system date or provided), input format, and whether to handle invalid dates. State assumptions clearly.
Create a helper function that converts a date (year, month, day) to the number of days since a fixed epoch, accounting for leap years and month lengths.
Write a function to check if a year is a leap year (divisible by 4, except centuries unless divisible by 400) and precompute cumulative days per month.
Subtract the two day counts, take absolute value if needed, and handle cases like same date (0 days) and future dates (negative or error).
Verify with examples like 2000-01-01 to 2000-01-02 (1 day) and across leap years (e.g., 2020-02-28 to 2020-03-01 = 2 days).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.