2026-07-02 •ᴗ• Practice note: This version should match the core of the reported interview question by about 85%-90%. 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[]): stringtargetDate = "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.
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.
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.
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.
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?
- Maximum Product New RatingOA · Seen Jul 2026
- Permutation SorterOA · Seen Jul 2026
- Get Distinct Pairs (Also apply to AS intern)Seen Jul 2026
- Maximum Final ValueSeen Jul 2026
- Minimum Delivery Center InconvenienceOA · Seen Jun 2026
- Unfulfilled Customers by Inventory PriorityOA · Seen Jun 2026
- Minimum Operations to Make the Integer ZeroSeen Jun 2026
- Create Array Generator ServiceSeen Jun 2026
public String findClosestVersionDate(String targetDate, String[] versions) {
// write your code here
}