Count Days Spent Together - Problem
Count Days Spent Together - A Date Intersection Problem

Alice and Bob are traveling to Rome for separate business meetings. You need to find how many days they'll be in the city at the same time.

Given four date strings in "MM-DD" format:
โ€ข arriveAlice & leaveAlice - Alice's arrival and departure dates
โ€ข arriveBob & leaveBob - Bob's arrival and departure dates

Goal: Return the total number of days both Alice and Bob are in Rome together (inclusive of arrival and departure dates).

Important: All dates are in the same non-leap year. Month lengths: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

Example: If Alice is there "08-15" to "08-18" and Bob is there "08-16" to "08-19", they overlap on "08-16", "08-17", "08-18" = 3 days.

Input & Output

example_1.py โ€” Basic Overlap
$ Input: arriveAlice = "08-15", leaveAlice = "08-18", arriveBob = "08-16", leaveBob = "08-19"
โ€บ Output: 3
๐Ÿ’ก Note: Alice is in Rome from Aug 15-18, Bob is there from Aug 16-19. They overlap on Aug 16, 17, and 18 = 3 days total.
example_2.py โ€” No Overlap
$ Input: arriveAlice = "10-01", leaveAlice = "10-31", arriveBob = "11-01", leaveBob = "12-31"
โ€บ Output: 0
๐Ÿ’ก Note: Alice leaves on Oct 31 and Bob arrives on Nov 1. There are no overlapping days.
example_3.py โ€” Complete Overlap
$ Input: arriveAlice = "09-01", leaveAlice = "10-19", arriveBob = "09-04", leaveBob = "09-09"
โ€บ Output: 6
๐Ÿ’ก Note: Bob's entire stay (Sep 4-9) falls within Alice's stay (Sep 1 - Oct 19). Bob is there for 6 days total.

Constraints

  • All dates are valid and in the format MM-DD
  • All dates occur in the same non-leap year
  • 1 โ‰ค MM โ‰ค 12 and 1 โ‰ค DD โ‰ค DaysInMonth[MM]
  • arriveAlice โ‰ค leaveAlice and arriveBob โ‰ค leaveBob
  • Month lengths: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

Visualization

Tap to expand
Date Range Intersection ProblemHotel Stay Timeline (August 2024):Aug 13Aug 15Aug 17Aug 19Aug 21Aug 23๐Ÿจ Alice: Aug 15-18๐Ÿจ Bob: Aug 16-19โœ… Both: Aug 16-18Mathematical Approach:โ€ข Convert dates to numbers: Aug 15 โ†’ Day 227, Aug 16 โ†’ Day 228, etc.โ€ข Find overlap start: max(Alice_start, Bob_start) = max(227, 228) = 228โ€ข Find overlap end: min(Alice_end, Bob_end) = min(230, 231) = 230โ€ข Calculate days: 230 - 228 + 1 = 3 days together3Days Together
Understanding the Visualization
1
Parse Dates
Convert MM-DD strings to day numbers (Jan 1 = Day 1, Dec 31 = Day 365)
2
Map Ranges
Visualize Alice's and Bob's stays as ranges on a timeline
3
Find Intersection
The overlap starts at the later arrival and ends at the earlier departure
4
Count Days
Calculate days = end - start + 1, or 0 if no overlap exists
Key Takeaway
๐ŸŽฏ Key Insight: Date overlap is just range intersection math - convert to numbers first, then use max/min operations to find the overlapping period efficiently in O(1) time!
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 5
28.5K Views
Medium Frequency
~15 min Avg. Time
892 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