Magicsheet logo

Number of Valid Clock Times

Easy
25%
Updated 8/1/2025

Asked by 1 Company

Number of Valid Clock Times

What is this problem about?

The Number of Valid Clock Times problem gives you a time string in "HH:MM" format where some digits are replaced with '?'. Count how many valid 24-hour clock times (00:00 to 23:59) could replace the '?' digits. This easy coding problem uses enumeration of all 1440 possible clock times and validation.

Why is this asked in interviews?

Google asks this easy problem to test clean enumeration with constraint checking. The approach: iterate all 24 * 60 = 1440 possible times, format each as "HH:MM", and check if it matches the pattern (each non-'?' character must match exactly). The enumeration and string interview pattern is demonstrated.

Algorithmic pattern used

Exhaustive enumeration over 1440 minutes. For hours h=0..23 and minutes m=0..59: format as f"{h:02d}:{m:02d}". Check against the pattern — for each non-'?' position, the formatted digit must equal the pattern digit. Count matches.

Example explanation

time = "?5:00". Check all HH:

  • 05:00 → "05" at "?5": digit 0='0'≠'?' and digit 1='5'=='5' ✓. Valid.
  • 15:00 → "15": '1' and '5'='5' ✓. Valid.
  • 25:00 → "25": invalid (hour ≥ 24). Other hours XX:00 where second digit is 5: 05, 15 → 2 valid. Answer = 2.

Common mistakes candidates make

  • Manually computing the count by analyzing each '?' position (error-prone).
  • Forgetting to zero-pad hours and minutes (use 2-digit format).
  • Checking wrong positions in the string (note the ':' at index 2).
  • Not iterating all 1440 possibilities (only iterating hours or minutes separately).

Interview preparation tip

Clock time enumeration problems have a small fixed search space (1440 times). Brute-force enumeration over all valid times with a pattern check is cleaner and less error-prone than manually computing based on '?' positions. Practice this "small fixed space = full enumeration" approach for time, date, and coordinate problems. Reserve mathematical analysis for when enumeration would be too slow.

Similar Questions