Day of the Year

EasyMathmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a Gregorian calendar date as a string date in the format YYYY-MM-DD. Return which day of its year that date is, counting January 1st as day 1. A year is a leap year (February has 29 days) if it is divisible by 4, except years divisible by 100, which are leap years only when also divisible by 400. So 2000 is a leap year but 1900 is not.

Examples

Input: date = "2019-01-09"
Output: 9
It is the ninth day of January.
Input: date = "2019-02-10"
Output: 41
January contributes 31 days, plus 10 days of February.
Input: date = "2004-03-01"
Output: 61
2004 is a leap year: 31 + 29 + 1 = 61.

Constraints

  • `date.length == 10`
  • `date[4] == date[7] == '-'`, all other characters are digits
  • `date` is a valid calendar date between 1900-01-01 and 2100-12-31

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems