Next Closest Time

MediumStringssimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a valid 24-hour time time in the form "HH:MM" (from "00:00" to "23:59"). Using only the digits that already appear in time (each digit may be reused any number of times), form the valid time that comes **soonest after** time, moving forward on the clock and wrapping past midnight if needed. If no other time can be formed, the answer is time itself, a full day later. Return that time as "HH:MM".

Examples

Input: time = "19:34"
Output: "19:39"
The digits are 1, 9, 3, 4. `19:39` is only five minutes later; `19:3x` with `x` in 5..8 cannot be built.
Input: time = "23:59"
Output: "22:22"
No later time today works, so wrap to tomorrow; the earliest time built from 2, 3, 5, 9 is `22:22`.

Constraints

  • `time.length == 5`
  • `time` is a valid time in the form `"HH:MM"`
  • `0 <= HH <= 23`, `0 <= MM <= 59`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems