Problem · String
Maximum Time
Learn this problemProblem statement
You are given a string that represents time in the format hh:mm. Some of the digits are blank (represented by ?). Fill in ? such that the time represented by this string is the maximum possible. Maximum time: 23:59, minimum time: 00:00. You can assume that input string is always valid.
Function
maximumTime(time: String) → StringExamples
Example 1
time = "?4:5?"return = "14:59"The leading digit must be 1 because the fixed hour ones digit is 4. The last minute digit can be 9.
Example 2
time = "23:5?"return = "23:59"The fixed hour is already maximal, and the final minute digit can be 9.
Example 3
time = "?2:22"return = "22:22"The second hour digit is fixed as 2, so choosing 2 for the first digit gives the maximum valid hour, 22.
Example 4
time = "0?:??"return = "09:59"With a fixed leading 0, the largest hour ones digit is 9; the largest minute is 59.
Example 5
time = "??:??"return = "23:59"All digits are unknown, so the maximum valid 24-hour time is 23:59.
Constraints
time.length = 5andtime[2] = ':'.- Every other character is a decimal digit or
?. - At least one replacement of all question marks produces a valid time from
00:00through23:59.
More Google problems
- Deduplicate Logs: Keep FirstONSITE INTERVIEW · Seen Jul 2026
- Deduplicate Logs: Keep LatestONSITE INTERVIEW · Seen Jul 2026
- Find a Template Across Binary-Tree LeavesONSITE INTERVIEW · Seen Jul 2026
- Maximum Programmer-Problem MatchingONSITE INTERVIEW · Seen Jul 2026
- Minimum Direction ViolationsONSITE INTERVIEW · Seen Jul 2026
- Stream Latest Log VersionsONSITE INTERVIEW · Seen Jul 2026
- Stream Unique Logs in Timestamp OrderONSITE INTERVIEW · Seen Jul 2026
- Top-K IP Addresses from File RecordsONSITE INTERVIEW · Seen Jul 2026