Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
You are given a string of length 5 called time, representing the current time on a digital clock in the format "hh:mm". The earliest possible time is "00:00" and the latest possible time is "23:59".
In the string time, the digits represented by the ? symbol are unknown, and must be replaced with a digit from 0 to 9.
Return an integer answer, the number of valid clock times that can be created by replacing every ? with a digit from 0 to 9.
Example 1:
Input: time = "?5:00" Output: 2 Explanation: We can replace the ? with either a 0 or 1, producing "05:00" or "15:00". Note that we cannot replace it with a 2, since the time "25:00" is invalid. In total, we have two choices.
Example 2:
Input: time = "0?:0?" Output: 100 Explanation: Each ? can be replaced by any digit from 0 to 9, so we have 100 total choices.
Example 3:
Input: time = "??:??" Output: 1440 Explanation: There are 24 possible choices for the hours, and 60 possible choices for the minutes. In total, we have 24 * 60 = 1440 choices.
Constraints:
time is a valid string of length 5 in the format "hh:mm"."00" <= hh <= "23""00" <= mm <= "59"'?' and need to be replaced with digits from 0 to 9.Problem summary: You are given a string of length 5 called time, representing the current time on a digital clock in the format "hh:mm". The earliest possible time is "00:00" and the latest possible time is "23:59". In the string time, the digits represented by the ? symbol are unknown, and must be replaced with a digit from 0 to 9. Return an integer answer, the number of valid clock times that can be created by replacing every ? with a digit from 0 to 9.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"?5:00"
"0?:0?"
"??:??"
largest-time-for-given-digits)latest-time-by-replacing-hidden-digits)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2437: Number of Valid Clock Times
class Solution {
public int countTime(String time) {
int ans = 0;
for (int h = 0; h < 24; ++h) {
for (int m = 0; m < 60; ++m) {
String s = String.format("%02d:%02d", h, m);
int ok = 1;
for (int i = 0; i < 5; ++i) {
if (s.charAt(i) != time.charAt(i) && time.charAt(i) != '?') {
ok = 0;
break;
}
}
ans += ok;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2437: Number of Valid Clock Times
func countTime(time string) int {
ans := 0
for h := 0; h < 24; h++ {
for m := 0; m < 60; m++ {
s := fmt.Sprintf("%02d:%02d", h, m)
ok := 1
for i := 0; i < 5; i++ {
if s[i] != time[i] && time[i] != '?' {
ok = 0
break
}
}
ans += ok
}
}
return ans
}
# Accepted solution for LeetCode #2437: Number of Valid Clock Times
class Solution:
def countTime(self, time: str) -> int:
def check(s: str, t: str) -> bool:
return all(a == b or b == '?' for a, b in zip(s, t))
return sum(
check(f'{h:02d}:{m:02d}', time) for h in range(24) for m in range(60)
)
// Accepted solution for LeetCode #2437: Number of Valid Clock Times
impl Solution {
pub fn count_time(time: String) -> i32 {
let mut ans = 0;
for i in 0..24 {
for j in 0..60 {
let mut ok = true;
let t = format!("{:02}:{:02}", i, j);
for (k, ch) in time.chars().enumerate() {
if ch != '?' && ch != t.chars().nth(k).unwrap() {
ok = false;
}
}
if ok {
ans += 1;
}
}
}
ans
}
}
// Accepted solution for LeetCode #2437: Number of Valid Clock Times
function countTime(time: string): number {
let ans = 0;
for (let h = 0; h < 24; ++h) {
for (let m = 0; m < 60; ++m) {
const s = `${h}`.padStart(2, '0') + ':' + `${m}`.padStart(2, '0');
let ok = 1;
for (let i = 0; i < 5; ++i) {
if (s[i] !== time[i] && time[i] !== '?') {
ok = 0;
break;
}
}
ans += ok;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.