Closest Version Date
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Amazon OA reported in July 2026 hands you a target date and up to 100000 unsorted release dates, and asks for the closest one with ties going to the later date. It's tagged SDE II, so don't expect a trick-free freebie. The core is a clean linear scan with date math, and the follow-up pushes you toward binary search. If you blank on the date conversion or the tie rule, StealthCoder runs invisibly on your screen during the live assessment and gives you a working solution. Still, this one is very learnable in an evening.
The problem
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[]): string Function findClosestVersionDate(targetDate: String, versions: String[]) → String Examples 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 <= 100000 targetDate and every date in versions are valid dates in YYYY-MM-DD format. versions may 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?
Reported by candidates. Source: FastPrep
Pattern and pitfall
Convert every YYYY-MM-DD string into a day number, then compare absolute differences. Don't hand-roll month lengths and leap years unless you have to. A built-in date parser or a days-from-civil formula works. Scan once, track the best distance, and on a tie pick the later date. The classic pitfall is comparing strings by difference, or breaking ties the wrong way. Example 3 exists to catch that. Another trap is returning early on a first match and missing that exact matches have distance 0, which is fine, but ties still matter elsewhere. For the follow-up, sort once or use the sorted list, then binary search for the insertion point of the target and check only the neighbors on each side. That gives O(log n) per query. If the live OA stalls you on leap-year math, StealthCoder is the hedge that keeps you moving.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Closest Version Date cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Closest Version Date FAQ
How hard is the Amazon Closest Version Date question really?+
Easy to medium. The base problem is a single pass with date conversion. The difficulty is in details: parsing dates correctly, handling leap years, and the tie rule that returns the later date. The follow-up on sorted input and many queries is where the SDE II bar shows up.
What's the trick to solving it fast?+
Turn each date into an integer day count, then it's just closest-number-to-target. Track the minimum absolute difference, and when distances are equal, keep the larger day value. Don't compare date strings directly for distance, only for ordering.
How do I handle the date conversion if I can't remember leap year rules?+
Use the language's built-in date library to get days since epoch. If you must do it by hand, leap years are divisible by 4, except centuries, unless divisible by 400. A days-from-civil formula avoids looping over months and years entirely.
How should I answer the sorted-list follow-up?+
Binary search for the first date greater than or equal to the target. The answer is either that date or the one just before it. Compare both distances, and on a tie return the later one. That's O(log n) per query after the dates are converted.
How do I prepare in 48 hours?+
Write the linear scan once with date parsing, then test all four examples, especially the tie and the exact match. Then write the binary search version on a sorted array of day numbers. Practice the closest-element-in-sorted-array pattern and you're covered.