Next Closest Clock Time Reusing Only the Current Digits
Last updated: November 29, 2026
Quick Overview
Given a time "HH:MM" in 24-hour format, return the next time (wrapping past midnight if needed) that can be formed using only the digits already present, each digit reusable any number of times.
0
0
0 solved
Given a time "HH:MM" in 24-hour format, return the next time (wrapping past midnight if needed) that can be formed using only the digits already present, each digit reusable any number of times.
A long-standing Google screen favorite because it has a tiny search space that rewards reasoning about it rather than clever tricks. Interviewers watch for a clean argument about why brute force is fine, correct validation of hours and minutes, and handling of the wraparound case.
What the Interviewer Expects
- Observes the search space is at most 4^4 = 256 candidates or 1440 minutes
- Chooses a simple, verifiable approach over a fragile digit-by-digit greedy
- Validates hour < 24 and minute < 60
- Handles wrap past midnight and the all-same-digit case
- Formats output with leading zeros
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the previous closest time instead.
- What if the clock shows seconds as well (HH:MM:SS)?
- Can you do it greedily by changing the rightmost digit that can be increased? When does that work?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: valid time string
HH:MM. - Allowed digits: the set of digits in the input, reusable.
- Output: the next strictly later time in circular order; if none differs, the same time (a full day lat...
High-Level Design
Convert the time to minutes since midnight. Step forward one minute at a time, up to 1440 steps, wrapping modulo 1440. For each candidate, format it as four digits and check that every digit is in the...