Next Upcoming Birthday
Problem statement
A family record keeps each person's name and birth date: person i is names[i], born on birthDates[i]. Build the lookup for the next upcoming birthday: for each date in queryDates, return the name of the person whose next birthday comes first.
- A person's birthday falls on the same month and day every year.
- A birthday on the query date itself counts as the next birthday.
- If every birthday has already passed in the query's year, look at the next year.
- If several people share the soonest birthday, return the name that comes first alphabetically.
Return one name per query, in the same order as queryDates.
Function
nextBirthdays(names: String[], birthDates: String[], queryDates: String[]) → String[]Examples
Example 1
names = ["Maya","Arjun","Lena"]birthDates = ["1990-03-15","1985-11-02","2001-07-30"]queryDates = ["2026-04-01","2026-11-02","2026-12-25"]return = ["Lena","Arjun","Maya"]On 2026-04-01 the next birthdays are Lena on 2026-07-30, Arjun on 2026-11-02 and Maya on 2027-03-15, so Lena is first. On 2026-11-02 it is Arjun's birthday that very day, which counts. On 2026-12-25 everyone's birthday has passed for the year, and the earliest one next year is Maya's on 2027-03-15.
Example 2
names = ["Zoe","Adam"]birthDates = ["1999-05-05","2003-05-05"]queryDates = ["2026-05-06"]return = ["Adam"]Both were born on May 5, which was the day before the query, so both next birthdays are on 2027-05-05. The tie goes to the name that comes first alphabetically, "Adam".
Example 3
names = ["Omar"]birthDates = ["1970-02-28"]queryDates = ["2028-02-29"]return = ["Omar"]2028 is a leap year and the query is February 29, one day after Omar's birthday. His next birthday is 2029-02-28.
Constraints
1 <= names.length <= 1000andbirthDates.length == names.length1 <= queryDates.length <= 1000- Every name is different and is one capital letter followed by 1 to 9 lowercase letters.
- Every date is a real calendar date written as
"YYYY-MM-DD". - Birth years are from
1900to2025, and nobody is born on February 29. - Query years are from
2000to2100, and every query date is on or after every birth date.