Voting Eligibility - Problem

Given a person's birth date as a string in the format YYYY-MM-DD, determine if they are eligible to vote. A person is eligible to vote if they are 18 years or older.

If the person is eligible, return "Eligible". If they are not eligible, return the number of days until they become eligible as a string (e.g., "120").

Assume the current date is 2024-01-01 for all calculations.

Input & Output

Example 1 — Not Eligible Yet
$ Input: birthDate = "2010-06-15"
Output: "1627"
💡 Note: Person born on 2010-06-15 will turn 18 on 2028-06-15. From current date 2024-01-01 to 2028-06-15 is 1627 days.
Example 2 — Just Became Eligible
$ Input: birthDate = "2006-01-01"
Output: "Eligible"
💡 Note: Person born on 2006-01-01 turned 18 on 2024-01-01, which is exactly the current date. They are eligible to vote.
Example 3 — Long Time Eligible
$ Input: birthDate = "1990-03-20"
Output: "Eligible"
💡 Note: Person born on 1990-03-20 turned 18 on 2008-03-20, which is long before current date 2024-01-01. They are eligible.

Constraints

  • Birth date is in valid YYYY-MM-DD format
  • Birth date is not in the future (before 2024-01-01)
  • 1900 ≤ birth year ≤ 2023

Visualization

Tap to expand
INPUTALGORITHMRESULTBirth Date"2010-06-15"Current Date2024-01-011Parse birth dateConvert string to date object2Calculate 18th birthdayAdd 18 years: 2028-06-153Compare dates2024-01-01 < 2028-06-154Calculate daysDays until eligible: 1627Not EligibleDays until eligible:"1627"Key Insight:Instead of counting age day by day, calculate the target eligibility date (18th birthday) and compare it directly with today.TutorialsPoint - Voting Eligibility | Direct Date Comparison
Asked in
Microsoft 15 Amazon 12 Google 8
12.5K Views
Medium Frequency
~10 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen