Ctrl K

Latest Time on a Broken Display

Easy
An old display shows the time as "HH:MM" on a 12-hour clock: hours run from 00 to 11 and minutes from 00 to 59. The earliest time is 00:00 and the latest is 11:59. Some segments have burned out, and each unreadable digit appears as ? in the string display. Replace every ? with a digit so that the result is a valid time, and make that time as late as possible. Return the resulting string.
Example 1
display="0?:?7" → '09:57'

The hour starts with 0, so 09 is the best we can do. The tens of minutes become 5, giving 09:57.

Example 2
display="??:3?" → '11:39'

The latest hour is 11, and 3? becomes 39.

Example 3
display="?2:00" → '02:00'

The second hour digit is 2, so the first one can only be 0: 12 is out of range, since the clock tops out at 11.

Constraints
  • len(display) == 5
  • display[2] == ':'
  • Every other character is a digit or ?
  • At least one valid time between 00:00 and 11:59 can be produced
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNearest Short Hex Color EasyStrike Out a Digit for the Maximum EasyHighest Letter in Both Cases EasyA Clock With Blank Digits EasyNext Time From the Same Digits Medium
loading Python…
Input
display="0?:?7"
Expected
'09:57'

Run checks the first 3 tests in your browser. Submit checks all 75 tests on the server, hidden ones included.