Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
Alice and Bob are traveling to Rome for separate business meetings.
You are given 4 strings arriveAlice, leaveAlice, arriveBob, and leaveBob. Alice will be in the city from the dates arriveAlice to leaveAlice (inclusive), while Bob will be in the city from the dates arriveBob to leaveBob (inclusive). Each will be a 5-character string in the format "MM-DD", corresponding to the month and day of the date.
Return the total number of days that Alice and Bob are in Rome together.
You can assume that all dates occur in the same calendar year, which is not a leap year. Note that the number of days per month can be represented as: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31].
Example 1:
Input: arriveAlice = "08-15", leaveAlice = "08-18", arriveBob = "08-16", leaveBob = "08-19" Output: 3 Explanation: Alice will be in Rome from August 15 to August 18. Bob will be in Rome from August 16 to August 19. They are both in Rome together on August 16th, 17th, and 18th, so the answer is 3.
Example 2:
Input: arriveAlice = "10-01", leaveAlice = "10-31", arriveBob = "11-01", leaveBob = "12-31" Output: 0 Explanation: There is no day when Alice and Bob are in Rome together, so we return 0.
Constraints:
"MM-DD".Problem summary: Alice and Bob are traveling to Rome for separate business meetings. You are given 4 strings arriveAlice, leaveAlice, arriveBob, and leaveBob. Alice will be in the city from the dates arriveAlice to leaveAlice (inclusive), while Bob will be in the city from the dates arriveBob to leaveBob (inclusive). Each will be a 5-character string in the format "MM-DD", corresponding to the month and day of the date. Return the total number of days that Alice and Bob are in Rome together. You can assume that all dates occur in the same calendar year, which is not a leap year. Note that the number of days per month can be represented as: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
"08-15" "08-18" "08-16" "08-19"
"10-01" "10-31" "11-01" "12-31"
number-of-days-between-two-dates)minimum-number-of-operations-to-convert-time)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2409: Count Days Spent Together
class Solution {
private int[] days = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public int countDaysTogether(
String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) {
String a = arriveAlice.compareTo(arriveBob) < 0 ? arriveBob : arriveAlice;
String b = leaveAlice.compareTo(leaveBob) < 0 ? leaveAlice : leaveBob;
int x = f(a), y = f(b);
return Math.max(y - x + 1, 0);
}
private int f(String s) {
int i = Integer.parseInt(s.substring(0, 2)) - 1;
int res = 0;
for (int j = 0; j < i; ++j) {
res += days[j];
}
res += Integer.parseInt(s.substring(3));
return res;
}
}
// Accepted solution for LeetCode #2409: Count Days Spent Together
func countDaysTogether(arriveAlice string, leaveAlice string, arriveBob string, leaveBob string) int {
days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
f := func(s string) int {
m, _ := strconv.Atoi(s[:2])
d, _ := strconv.Atoi(s[3:])
res := 0
for i := 0; i < m-1; i++ {
res += days[i]
}
res += d
return res
}
a, b := arriveAlice, leaveBob
if arriveAlice < arriveBob {
a = arriveBob
}
if leaveAlice < leaveBob {
b = leaveAlice
}
x, y := f(a), f(b)
ans := y - x + 1
if ans < 0 {
return 0
}
return ans
}
# Accepted solution for LeetCode #2409: Count Days Spent Together
class Solution:
def countDaysTogether(
self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str
) -> int:
a = max(arriveAlice, arriveBob)
b = min(leaveAlice, leaveBob)
days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
x = sum(days[: int(a[:2]) - 1]) + int(a[3:])
y = sum(days[: int(b[:2]) - 1]) + int(b[3:])
return max(y - x + 1, 0)
// Accepted solution for LeetCode #2409: Count Days Spent Together
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2409: Count Days Spent Together
// class Solution {
// private int[] days = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//
// public int countDaysTogether(
// String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) {
// String a = arriveAlice.compareTo(arriveBob) < 0 ? arriveBob : arriveAlice;
// String b = leaveAlice.compareTo(leaveBob) < 0 ? leaveAlice : leaveBob;
// int x = f(a), y = f(b);
// return Math.max(y - x + 1, 0);
// }
//
// private int f(String s) {
// int i = Integer.parseInt(s.substring(0, 2)) - 1;
// int res = 0;
// for (int j = 0; j < i; ++j) {
// res += days[j];
// }
// res += Integer.parseInt(s.substring(3));
// return res;
// }
// }
// Accepted solution for LeetCode #2409: Count Days Spent Together
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2409: Count Days Spent Together
// class Solution {
// private int[] days = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//
// public int countDaysTogether(
// String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) {
// String a = arriveAlice.compareTo(arriveBob) < 0 ? arriveBob : arriveAlice;
// String b = leaveAlice.compareTo(leaveBob) < 0 ? leaveAlice : leaveBob;
// int x = f(a), y = f(b);
// return Math.max(y - x + 1, 0);
// }
//
// private int f(String s) {
// int i = Integer.parseInt(s.substring(0, 2)) - 1;
// int res = 0;
// for (int j = 0; j < i; ++j) {
// res += days[j];
// }
// res += Integer.parseInt(s.substring(3));
// return res;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.