← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
May 2026

Summary

Amazon SWE coding round with one problem that looks simple on the surface but has enough edge cases to trip you up if you're not careful. The no-built-in-date-functions constraint is the whole point.

Questions Asked (1)

Q1

Given a date string in 'YYYY-MM-DD' format, write a function that returns how many days have passed from that date to today. You cannot use any built-in date or time libraries.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The constraint is what makes this annoying.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

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.

1. Clarify requirements and constraints

Confirm the definition of 'today' (e.g., system date or provided), input format, and whether to handle invalid dates. State assumptions clearly.

2. Design a date-to-days conversion

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.

3. Implement leap year and month day logic

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.

4. Compute the difference and handle edge cases

Subtract the two day counts, take absolute value if needed, and handle cases like same date (0 days) and future dates (negative or error).

5. Test with known dates

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).

Key Points to Mention

  • Leap year rules: divisible by 4, but not by 100 unless also by 400.
  • Cumulative days per month array, adjusted for leap years.
  • Epoch-based calculation: days since a fixed date (e.g., 0000-01-01 or 1970-01-01).
  • Handling of invalid dates (e.g., month > 12, day > days in month).
  • Time complexity: O(1) with constant-time arithmetic.
  • Testability: allowing 'today' to be injected for unit tests.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.