Number of Days Between Two Dates

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 two valid Gregorian dates date1 and date2, each written as YYYY-MM-DD. Return the absolute number of days between them. The two dates may be in either order, and the answer is 0 when they are equal. Leap years follow the usual rule: divisible by 4, except years divisible by 100 unless they are also divisible by 400.

Examples

Input: date1 = "2019-06-29", date2 = "2019-06-30"
Output: 1
The dates are consecutive days.
Input: date1 = "2020-01-15", date2 = "2019-12-31"
Output: 15
The later date comes first; 1 day finishes 2019 and 14 more reach January 15.

Constraints

  • The given dates are valid dates between the years 1971 and 2100 (inclusive)

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