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 date, return the corresponding day of the week for that date.
The input is given as three integers representing the day, month and year respectively.
Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.
Note: January 1, 1971 was a Friday.
Example 1:
Input: day = 31, month = 8, year = 2019 Output: "Saturday"
Example 2:
Input: day = 18, month = 7, year = 1999 Output: "Sunday"
Example 3:
Input: day = 15, month = 8, year = 1993 Output: "Sunday"
Constraints:
1971 and 2100.Problem summary: Given a date, return the corresponding day of the week for that date. The input is given as three integers representing the day, month and year respectively. Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}. Note: January 1, 1971 was a Friday.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
31 8 2019
18 7 1999
15 8 1993
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1185: Day of the Week
import java.util.Calendar;
class Solution {
private static final String[] WEEK
= {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
public static String dayOfTheWeek(int day, int month, int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day);
return WEEK[calendar.get(Calendar.DAY_OF_WEEK) - 1];
}
}
// Accepted solution for LeetCode #1185: Day of the Week
func dayOfTheWeek(d int, m int, y int) string {
if m < 3 {
m += 12
y -= 1
}
c := y / 100
y %= 100
w := (c/4 - 2*c + y + y/4 + 13*(m+1)/5 + d - 1) % 7
weeks := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
return weeks[(w+7)%7]
}
# Accepted solution for LeetCode #1185: Day of the Week
class Solution:
def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
return datetime.date(year, month, day).strftime('%A')
// Accepted solution for LeetCode #1185: Day of the Week
struct Solution;
impl Solution {
fn day_of_the_week(day: i32, mut month: i32, mut year: i32) -> String {
let days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
if month < 3 {
month += 12;
year -= 1;
}
let week =
(day + ((month + 1) * 26) / 10 + year + year / 4 + 6 * (year / 100) + year / 400 + 5)
% 7
+ 1;
days[week as usize % 7].to_string()
}
}
#[test]
fn test() {
let day = 31;
let month = 8;
let year = 2019;
let res = "Saturday".to_string();
assert_eq!(Solution::day_of_the_week(day, month, year), res);
let day = 18;
let month = 7;
let year = 1999;
let res = "Sunday".to_string();
assert_eq!(Solution::day_of_the_week(day, month, year), res);
let day = 15;
let month = 8;
let year = 1993;
let res = "Sunday".to_string();
assert_eq!(Solution::day_of_the_week(day, month, year), res);
}
// Accepted solution for LeetCode #1185: Day of the Week
function dayOfTheWeek(d: number, m: number, y: number): string {
if (m < 3) {
m += 12;
y -= 1;
}
const c: number = (y / 100) | 0;
y %= 100;
const w = (((c / 4) | 0) - 2 * c + y + ((y / 4) | 0) + (((13 * (m + 1)) / 5) | 0) + d - 1) % 7;
const weeks: string[] = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
return weeks[(w + 7) % 7];
}
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.