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.
Move from brute-force thinking to an efficient approach using math strategy.
Given two numbers, hour and minutes, return the smaller angle (in degrees) formed between the hour and the minute hand.
Answers within 10-5 of the actual value will be accepted as correct.
Example 1:
Input: hour = 12, minutes = 30 Output: 165
Example 2:
Input: hour = 3, minutes = 30 Output: 75
Example 3:
Input: hour = 3, minutes = 15 Output: 7.5
Constraints:
1 <= hour <= 120 <= minutes <= 59Problem summary: Given two numbers, hour and minutes, return the smaller angle (in degrees) formed between the hour and the minute hand. Answers within 10-5 of the actual value will be accepted as correct.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
12 30
3 30
3 15
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1344: Angle Between Hands of a Clock
class Solution {
public double angleClock(int hour, int minutes) {
double h = 30 * hour + 0.5 * minutes;
double m = 6 * minutes;
double diff = Math.abs(h - m);
return Math.min(diff, 360 - diff);
}
}
// Accepted solution for LeetCode #1344: Angle Between Hands of a Clock
func angleClock(hour int, minutes int) float64 {
h := 30*float64(hour) + 0.5*float64(minutes)
m := 6 * float64(minutes)
diff := math.Abs(h - m)
return math.Min(diff, 360-diff)
}
# Accepted solution for LeetCode #1344: Angle Between Hands of a Clock
class Solution:
def angleClock(self, hour: int, minutes: int) -> float:
h = 30 * hour + 0.5 * minutes
m = 6 * minutes
diff = abs(h - m)
return min(diff, 360 - diff)
// Accepted solution for LeetCode #1344: Angle Between Hands of a Clock
struct Solution;
impl Solution {
fn angle_clock(hour: i32, minutes: i32) -> f64 {
let h = ((hour % 12) as f64 + (minutes as f64 / 60.0)) * 30.0;
let m = minutes as f64 * 6.0;
let a = (h - m).abs();
if a > 180.0 {
360.0 - a
} else {
a
}
}
}
#[test]
fn test() {
use assert_approx_eq::assert_approx_eq;
let hour = 12;
let minutes = 30;
let res = 165.0;
assert_approx_eq!(Solution::angle_clock(hour, minutes), res);
let hour = 3;
let minutes = 30;
let res = 75.0;
assert_approx_eq!(Solution::angle_clock(hour, minutes), res);
let hour = 3;
let minutes = 15;
let res = 7.5;
assert_approx_eq!(Solution::angle_clock(hour, minutes), res);
let hour = 4;
let minutes = 50;
let res = 155.0;
assert_approx_eq!(Solution::angle_clock(hour, minutes), res);
let hour = 12;
let minutes = 0;
let res = 0.0;
assert_approx_eq!(Solution::angle_clock(hour, minutes), res);
let hour = 1;
let minutes = 57;
let res = 76.5;
assert_approx_eq!(Solution::angle_clock(hour, minutes), res);
}
// Accepted solution for LeetCode #1344: Angle Between Hands of a Clock
function angleClock(hour: number, minutes: number): number {
const h = 30 * hour + 0.5 * minutes;
const m = 6 * minutes;
const diff = Math.abs(h - m);
return Math.min(diff, 360 - diff);
}
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.