PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Optiver

Count the Days Between Two Dates

Last updated: Jul 2, 2026

Count the Days Between Two Dates

Company: Optiver

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

# Count the Days Between Two Dates You are given two calendar dates as strings in `YYYY-MM-DD` format. Return the number of days between them as a non-negative integer. You must compute the result **without using any built-in date/time or calendar library** (e.g., no `datetime`, `java.time`, `Date`, or similar). The only calendar knowledge you may rely on is the standard Gregorian calendar rules given below. Assume a helper that returns the number of days in a given month is something you write yourself. ## Calendar rules - Months have the usual lengths: January 31, February 28 (29 in a leap year), March 31, April 30, May 31, June 30, July 31, August 31, September 30, October 31, November 30, December 31. - A year is a **leap year** if it is divisible by 4, **except** years divisible by 100, **unless** they are also divisible by 400. So 2000 and 2024 are leap years; 1900 and 2100 are not. ## Input - `date1`: a string in `YYYY-MM-DD` format. - `date2`: a string in `YYYY-MM-DD` format. Both dates are guaranteed to be valid calendar dates between `1900-01-01` and `2100-12-31` inclusive. The dates may be given in either order (`date1` is not necessarily earlier than `date2`). ## Output Return a single integer: the absolute difference in days between the two dates. If the dates are equal, return `0`. ## Examples | date1 | date2 | Output | Explanation | |-------|-------|--------|-------------| | `2019-06-29` | `2019-06-30` | `1` | Consecutive days. | | `2019-12-31` | `2020-01-15` | `15` | Crosses a year boundary. | | `2020-02-28` | `2020-03-01` | `2` | 2020 is a leap year, so February 29 lies between them. | | `2019-02-28` | `2019-03-01` | `1` | 2019 is not a leap year. | | `2021-07-04` | `2021-07-04` | `0` | Same date. | ## Constraints - `1900-01-01 <= date1, date2 <= 2100-12-31` - Both inputs are valid dates in `YYYY-MM-DD` format. - No date/time library calls are allowed; leap-year handling must be implemented from the rules above.

Related Interview Questions

  • Build and Validate a Binary Tree from Parent-Child Pairs - Optiver (medium)
  • Days Between Two Calendar Dates - 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)
|Home/Coding & Algorithms/Optiver

Count the Days Between Two Dates

Optiver logo
Optiver
Jul 23, 2025, 12:00 AM
mediumSoftware EngineerTake-home ProjectCoding & Algorithms
0
0

Count the Days Between Two Dates

You are given two calendar dates as strings in YYYY-MM-DD format. Return the number of days between them as a non-negative integer.

You must compute the result without using any built-in date/time or calendar library (e.g., no datetime, java.time, Date, or similar). The only calendar knowledge you may rely on is the standard Gregorian calendar rules given below. Assume a helper that returns the number of days in a given month is something you write yourself.

Calendar rules

  • Months have the usual lengths: January 31, February 28 (29 in a leap year), March 31, April 30, May 31, June 30, July 31, August 31, September 30, October 31, November 30, December 31.
  • A year is a leap year if it is divisible by 4, except years divisible by 100, unless they are also divisible by 400. So 2000 and 2024 are leap years; 1900 and 2100 are not.

Input

  • date1 : a string in YYYY-MM-DD format.
  • date2 : a string in YYYY-MM-DD format.

Both dates are guaranteed to be valid calendar dates between 1900-01-01 and 2100-12-31 inclusive. The dates may be given in either order (date1 is not necessarily earlier than date2).

Output

Return a single integer: the absolute difference in days between the two dates. If the dates are equal, return 0.

Examples

date1date2OutputExplanation
2019-06-292019-06-301Consecutive days.
2019-12-312020-01-1515Crosses a year boundary.
2020-02-282020-03-0122020 is a leap year, so February 29 lies between them.
2019-02-282019-03-0112019 is not a leap year.
2021-07-042021-07-040Same date.

Constraints

  • 1900-01-01 <= date1, date2 <= 2100-12-31
  • Both inputs are valid dates in YYYY-MM-DD format.
  • No date/time library calls are allowed; leap-year handling must be implemented from the rules above.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Optiver•More Software Engineer•Optiver Software Engineer•Optiver Coding & Algorithms•Software Engineer Coding & Algorithms
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.