← Questions

Valid date from three integers in any order

0
GoogleCoding

A coding interview question asked at Google. It tests simulation, permutations and edge cases.

You are given three integers a, b and c. Their order is unknown: any one of them may be the year, the month or the day. This covers formats such as yyyy-MM-dd, MM-dd-yyyy and dd-MM-yyyy.

Implement:

def can_form_date(a: int, b: int, c: int) -> bool: ...
def all_valid_dates(a: int, b: int, c: int) -> list[tuple[int, int, int]]: ...
  • can_form_date returns True if at least one assignment of the three numbers to (year, month, day) is a real calendar date.
  • all_valid_dates returns every distinct valid (year, month, day), sorted chronologically.

A date is valid when:

  • 1 <= year <= 9999
  • 1 <= month <= 12
  • 1 <= day <= the number of days in that month of that year. February has 29 days in a leap year and 28 otherwise.
  • A leap year is divisible by 4, except century years, which must also be divisible by 400 (2000 is a leap year, 1900 is not).

Inputs may be zero, negative or larger than 9999. Such values are never valid in any role.

Example 1

Input:  a = 30, b = 1987, c = 7
can_form_date   -> True
all_valid_dates -> [(1987, 7, 30)]

Only year 1987, month 7, day 30 works. 30 and 1987 can't be months.

Example 2

Input:  a = 2, b = 1900, c = 29
can_form_date   -> False

February 29, 1900 does not exist, because 1900 is not a leap year. No other assignment has a month between 1 and 12 with a valid day and year.

Example 3

Input:  a = 6, b = 6, c = 2021
all_valid_dates -> [(2021, 6, 6)]

Two permutations give the same date, and it is listed once.

Clarifying questions

  • Are the three numbers allowed to repeat, and should repeats produce duplicate dates in the output?
  • Is a two-digit year like 21 year 21, or should it be read as 2021?
  • Should year 0 or negative years ever be valid?
  • Is the output order by full date (year, then month, then day)?

What a strong answer covers

  • Tries all 6 permutations as (year, month, day): constant work, so O(1).
  • Validates the month range first, then the day against the month length, with a correct leap-year rule (divisible by 4, not by 100 unless by 400).
  • Deduplicates with a set before sorting, so equal inputs like 6, 6, 2021 list one date.
  • Rejects zero, negative and over-9999 values in every role, and handles 29 February in 2000 versus 1900.
  • Tests: a single valid assignment, an impossible leap day, duplicates, several valid dates (for example 1, 2, 2000), and out-of-range inputs.

Follow-ups

  • Accept the input as a string with separators and unknown order, and parse it.
  • Return only dates within a given range, or the earliest valid one.
  • Support a fixed set of allowed formats instead of all permutations.

Where it's asked

What it tests

simulation · permutations · edge cases

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding