PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in date and calendar arithmetic, specifically handling varying month lengths, leap year rules, and computing absolute differences between dates.

  • medium
  • Optiver
  • Coding & Algorithms
  • Software Engineer

Days Between Two Calendar Dates

Company: Optiver

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

## Days Between Two Calendar Dates You are given two dates as strings, each in the format `YYYY-MM-DD`. Write a function that returns the number of days between the two dates as a non-negative integer (i.e., the absolute difference). You must account for the varying lengths of months and for leap years. Do **not** rely on built-in date/calendar libraries — implement the calendar arithmetic yourself. A year is a leap year if it is divisible by 4, except that years divisible by 100 are not leap years unless they are also divisible by 400 (so 2000 and 2020 are leap years, but 1900 and 2100 are not). In a leap year, February has 29 days; otherwise it has 28. ### Input - `date1`: a string in the format `YYYY-MM-DD` - `date2`: a string in the format `YYYY-MM-DD` Both strings are guaranteed to be well-formed, valid calendar dates. The dates may be given in either order (`date1` may be before, after, or equal to `date2`). ### Output Return a single integer: the absolute number of days between the two dates. Two equal dates have a difference of `0`. ### Examples **Example 1** ``` Input: date1 = "2019-12-31", date2 = "2020-01-15" Output: 15 ``` From December 31, 2019 to January 15, 2020 is 15 days. **Example 2** ``` Input: date1 = "2020-03-01", date2 = "2020-02-28" Output: 2 ``` 2020 is a leap year, so February 29, 2020 exists: February 28 → February 29 → March 1 is 2 days. **Example 3** ``` Input: date1 = "2021-02-28", date2 = "2021-03-01" Output: 1 ``` 2021 is not a leap year, so February 28 is immediately followed by March 1. **Example 4** ``` Input: date1 = "2020-05-05", date2 = "2020-05-05" Output: 0 ``` ### Constraints - Each date is a valid calendar date between `1900-01-01` and `2100-12-31` inclusive. - The answer fits comfortably in a 32-bit signed integer.

Quick Answer: This question evaluates proficiency in date and calendar arithmetic, specifically handling varying month lengths, leap year rules, and computing absolute differences between dates.

You are given two dates as strings, each in the format `YYYY-MM-DD`. Return the absolute number of days between the two dates as a non-negative integer. You must account for the varying lengths of months and for leap years, and you must implement the calendar arithmetic yourself (do not rely on built-in date/calendar libraries). **Leap-year rule:** A year is a leap year if it is divisible by 4, except that years divisible by 100 are not leap years unless they are also divisible by 400. So 2000 and 2020 are leap years, but 1900 and 2100 are not. In a leap year February has 29 days; otherwise it has 28. **Input:** - `date1`: a string in the format `YYYY-MM-DD` - `date2`: a string in the format `YYYY-MM-DD` Both strings are guaranteed to be well-formed, valid calendar dates. The dates may be given in either order (`date1` may be before, after, or equal to `date2`). **Output:** A single integer — the absolute number of days between the two dates. Two equal dates have a difference of `0`. **Example 1:** `date1 = "2019-12-31", date2 = "2020-01-15"` -> `15` **Example 2:** `date1 = "2020-03-01", date2 = "2020-02-28"` -> `2` (2020 is a leap year, so Feb 29 exists). **Example 3:** `date1 = "2021-02-28", date2 = "2021-03-01"` -> `1` (2021 is not a leap year). **Example 4:** `date1 = "2020-05-05", date2 = "2020-05-05"` -> `0`.

Constraints

  • Each date is a valid calendar date between 1900-01-01 and 2100-12-31 inclusive.
  • Both input strings are well-formed and in YYYY-MM-DD format.
  • The dates may be given in any order (date1 before, after, or equal to date2).
  • The answer fits comfortably in a 32-bit signed integer.

Examples

Input: ("2019-12-31", "2020-01-15")

Expected Output: 15

Explanation: From Dec 31, 2019 to Jan 15, 2020 is 15 days (crosses a year boundary).

Input: ("2020-03-01", "2020-02-28")

Expected Output: 2

Explanation: 2020 is a leap year: Feb 28 -> Feb 29 -> Mar 1 is 2 days. Order is reversed, so the absolute difference is used.

Input: ("2021-02-28", "2021-03-01")

Expected Output: 1

Explanation: 2021 is not a leap year, so Feb 28 is immediately followed by Mar 1 (1 day).

Input: ("2020-05-05", "2020-05-05")

Expected Output: 0

Explanation: Two equal dates have a difference of 0.

Input: ("1900-01-01", "2100-12-31")

Expected Output: 73413

Explanation: Full valid range span. Correctly counts leap years across two centuries (1900 and 2100 are NOT leap, 2000 IS leap).

Input: ("1900-02-28", "1900-03-01")

Expected Output: 1

Explanation: 1900 is divisible by 100 but not 400, so it is NOT a leap year: Feb 28 -> Mar 1 is 1 day.

Input: ("2000-02-28", "2000-03-01")

Expected Output: 2

Explanation: 2000 is divisible by 400, so it IS a leap year: Feb 28 -> Feb 29 -> Mar 1 is 2 days.

Input: ("2100-02-28", "2100-03-01")

Expected Output: 1

Explanation: 2100 is divisible by 100 but not 400, so it is NOT a leap year: Feb 28 -> Mar 1 is 1 day.

Hints

  1. Convert each date to a single integer 'ordinal' — the number of days from a fixed reference point — then take the absolute difference. This avoids messy per-month/per-year subtraction logic.
  2. To build the ordinal, sum the length of every full year before the given year (365 or 366), then add the length of every full month before the given month in that year, then add the day-of-month.
  3. Get the leap-year test exactly right: divisible by 4 AND (not divisible by 100 OR divisible by 400). Test it on 1900 (not leap), 2000 (leap), and 2100 (not leap).
Last updated: Jul 2, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Build and Validate a Binary Tree from Parent-Child Pairs - Optiver (medium)
  • Thread-Safe Stock Inventory: Buy and Sell Without Overselling - Optiver (medium)
  • Find missing numbers in sequences - Optiver (hard)
  • Design a circular queue data structure - Optiver (medium)
  • Optimize flight and cargo bookings for profit - Optiver (hard)