Closest Version Date
Learn this problemProblem statement
2026-07-02 •ᴗ• Practice note: This version should match the core of the reported interview question by about 85%-90%. It was reported for SDE II. It may not be word-for-word identical, but the main idea and expected approach are close.
You are given a target date and a list of release dates for different software versions. Return the release date that is closest to the target date. The distance between two dates is the absolute number of calendar days between them.
If two release dates are equally close to the target date, return the later date. All dates are provided in YYYY-MM-DD format.
Function Signature
function findClosestVersionDate(targetDate: string, versions: string[]): stringFunction
findClosestVersionDate(targetDate: String, versions: String[]) → StringExamples
Example 1
targetDate = "2026-04-01"versions = ["2023-04-01", "2025-04-01", "2026-05-03"]return = "2026-05-03"2023-04-01 is 1096 days away, 2025-04-01 is 365 days away, and 2026-05-03 is 32 days away. The closest release date is 2026-05-03.
Example 2
targetDate = "2024-06-10"versions = ["2024-06-01", "2024-06-20", "2024-07-01"]return = "2024-06-01"2024-06-01 is 9 days away, which is closer than 2024-06-20 and 2024-07-01.
Example 3
targetDate = "2025-01-15"versions = ["2025-01-10", "2025-01-20"]return = "2025-01-20"Both dates are 5 days away. Since there is a tie, return the later date.
Example 4
targetDate = "2026-04-01"versions = ["2026-04-01", "2026-05-03", "2025-12-31"]return = "2026-04-01"An exact match has distance 0, so it is the closest version date.
Constraints
1 <= versions.length <= 100000targetDateand every date inversionsare valid dates inYYYY-MM-DDformat.versionsmay not be sorted.
Follow-up
If versions is already sorted, can you solve each query faster than checking every date? What if there are many target dates queried against the same version list?