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 s representing a 12-hour format time where some of the digits (possibly none) are replaced with a "?".
12-hour times are formatted as "HH:MM", where HH is between 00 and 11, and MM is between 00 and 59. The earliest 12-hour time is 00:00, and the latest is 11:59.
You have to replace all the "?" characters in s with digits such that the time we obtain by the resulting string is a valid 12-hour format time and is the latest possible.
Return the resulting string.
Example 1:
Input: s = "1?:?4"
Output: "11:54"
Explanation: The latest 12-hour format time we can achieve by replacing "?" characters is "11:54".
Example 2:
Input: s = "0?:5?"
Output: "09:59"
Explanation: The latest 12-hour format time we can achieve by replacing "?" characters is "09:59".
Constraints:
s.length == 5s[2] is equal to the character ":".s[2] are digits or "?" characters."00:00" and "11:59" that you can obtain after replacing the "?" characters.Problem summary: You are given a string s representing a 12-hour format time where some of the digits (possibly none) are replaced with a "?". 12-hour times are formatted as "HH:MM", where HH is between 00 and 11, and MM is between 00 and 59. The earliest 12-hour time is 00:00, and the latest is 11:59. You have to replace all the "?" characters in s with digits such that the time we obtain by the resulting string is a valid 12-hour format time and is the latest possible. Return the resulting string.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"1?:?4"
"0?:5?"
latest-time-by-replacing-hidden-digits)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3114: Latest Time You Can Obtain After Replacing Characters
class Solution {
public String findLatestTime(String s) {
for (int h = 11;; h--) {
for (int m = 59; m >= 0; m--) {
String t = String.format("%02d:%02d", h, m);
boolean ok = true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != '?' && s.charAt(i) != t.charAt(i)) {
ok = false;
break;
}
}
if (ok) {
return t;
}
}
}
}
}
// Accepted solution for LeetCode #3114: Latest Time You Can Obtain After Replacing Characters
func findLatestTime(s string) string {
for h := 11; ; h-- {
for m := 59; m >= 0; m-- {
t := fmt.Sprintf("%02d:%02d", h, m)
ok := true
for i := 0; i < len(s); i++ {
if s[i] != '?' && s[i] != t[i] {
ok = false
break
}
}
if ok {
return t
}
}
}
}
# Accepted solution for LeetCode #3114: Latest Time You Can Obtain After Replacing Characters
class Solution:
def findLatestTime(self, s: str) -> str:
for h in range(11, -1, -1):
for m in range(59, -1, -1):
t = f"{h:02d}:{m:02d}"
if all(a == b for a, b in zip(s, t) if a != "?"):
return t
// Accepted solution for LeetCode #3114: Latest Time You Can Obtain After Replacing Characters
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3114: Latest Time You Can Obtain After Replacing Characters
// class Solution {
// public String findLatestTime(String s) {
// for (int h = 11;; h--) {
// for (int m = 59; m >= 0; m--) {
// String t = String.format("%02d:%02d", h, m);
// boolean ok = true;
// for (int i = 0; i < s.length(); i++) {
// if (s.charAt(i) != '?' && s.charAt(i) != t.charAt(i)) {
// ok = false;
// break;
// }
// }
// if (ok) {
// return t;
// }
// }
// }
// }
// }
// Accepted solution for LeetCode #3114: Latest Time You Can Obtain After Replacing Characters
function findLatestTime(s: string): string {
for (let h = 11; ; h--) {
for (let m = 59; m >= 0; m--) {
const t: string = `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}`;
let ok: boolean = true;
for (let i = 0; i < s.length; i++) {
if (s[i] !== '?' && s[i] !== t[i]) {
ok = false;
break;
}
}
if (ok) {
return t;
}
}
}
}
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.