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.
Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.
Example 1:
Input: date = "2019-01-09" Output: 9 Explanation: Given date is the 9th day of the year in 2019.
Example 2:
Input: date = "2019-02-10" Output: 41
Constraints:
date.length == 10date[4] == date[7] == '-', and all other date[i]'s are digitsdate represents a calendar date between Jan 1st, 1900 and Dec 31st, 2019.Problem summary: Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
"2019-01-09"
"2019-02-10"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1154: Day of the Year
class Solution {
public int dayOfYear(String date) {
int y = Integer.parseInt(date.substring(0, 4));
int m = Integer.parseInt(date.substring(5, 7));
int d = Integer.parseInt(date.substring(8));
int v = y % 400 == 0 || (y % 4 == 0 && y % 100 != 0) ? 29 : 28;
int[] days = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int ans = d;
for (int i = 0; i < m - 1; ++i) {
ans += days[i];
}
return ans;
}
}
// Accepted solution for LeetCode #1154: Day of the Year
func dayOfYear(date string) (ans int) {
var y, m, d int
fmt.Sscanf(date, "%d-%d-%d", &y, &m, &d)
days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
if y%400 == 0 || (y%4 == 0 && y%100 != 0) {
days[1] = 29
}
ans += d
for _, v := range days[:m-1] {
ans += v
}
return
}
# Accepted solution for LeetCode #1154: Day of the Year
class Solution:
def dayOfYear(self, date: str) -> int:
y, m, d = (int(s) for s in date.split('-'))
v = 29 if y % 400 == 0 or (y % 4 == 0 and y % 100) else 28
days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
return sum(days[: m - 1]) + d
// Accepted solution for LeetCode #1154: Day of the Year
struct Solution;
impl Solution {
fn day_of_year(date: String) -> i32 {
let a: Vec<&str> = date.split_terminator('-').collect();
let year = a[0].parse::<usize>().unwrap();
let month = a[1].parse::<usize>().unwrap();
let day = a[2].parse::<usize>().unwrap();
let mut days: Vec<usize> = vec![31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let mut sum = 0;
if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) {
days[1] += 1;
}
for i in 0..month - 1 {
sum += days[i];
}
sum += day;
sum as i32
}
}
#[test]
fn test() {
assert_eq!(Solution::day_of_year("2019-01-09".to_string()), 9);
assert_eq!(Solution::day_of_year("2019-02-10".to_string()), 41);
assert_eq!(Solution::day_of_year("2003-03-01".to_string()), 60);
assert_eq!(Solution::day_of_year("2004-03-01".to_string()), 61);
}
// Accepted solution for LeetCode #1154: Day of the Year
function dayOfYear(date: string): number {
const y = +date.slice(0, 4);
const m = +date.slice(5, 7);
const d = +date.slice(8);
const v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28;
const days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
return days.slice(0, m - 1).reduce((a, b) => a + b, d);
}
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.