FastPrepCan Three Numbers Form a Valid Date?

Can Three Numbers Form a Valid Date?

Google logoGoogle● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Given three positive integers, decide whether some permutation can represent a valid Gregorian date [year,month,day].

The year must be in [1,9999]. Apply ordinary Gregorian leap-year rules: divisible by 400, or divisible by 4 but not 100. Each input number is used exactly once.

Function

canFormValidDate(values: int[]) → boolean

Examples

Example 1

values = [2024,2,29]return = true

2024 is a leap year, so February 29 is valid.

Example 2

values = [31,4,2024]return = false

No permutation forms a valid date because April has only 30 days and the other placements fail month/year bounds.

Example 3

values = [12,25,2025]return = true

The permutation [2025,12,25] is valid.

Constraints

  • values.length = 3.
  • 1 <= values[i] <= 9999.

More Google problems

See Google hiring insights
public boolean canFormValidDate(int[] values) {
    // Write your code here.
}
values[2024,2,29]
expectedtrue
Checking account…