Valid Times on a Digital Clock
Reported by candidates from Toptal's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Toptal reported this one in September 2026, and it looks harder than it is. Four digits, one function, count the distinct valid HH:MM times. If you've got an OA coming, the first sentence of the constraints is your gift: every digit is 0-9 and there are only four of them. That means 24 permutations, total. No clever math needed. The only real trap is duplicates, like example 2 where 1,4,1,4 gives 3 and not 24 minus invalid ones. If you blank under the timer, StealthCoder runs invisibly on your screen and hands you the working solution live.
The problem
You are given four decimal digits a, b, c, and d. Arrange all four supplied digit occurrences into a 24-hour time HH:MM. A time is valid when 00 <= HH <= 23 and 00 <= MM <= 59. Leading zeroes are allowed. Return the number of distinct valid times that can be formed. Each supplied digit occurrence must be used exactly once; equal arrangements caused by repeated digits count only once. Function solution(a: int, b: int, c: int, d: int) → int Examples Example 1 a = 1 b = 2 c = 3 d = 4 return = 10 The digits form ten distinct valid times: 12:34, 12:43, 13:24, 13:42, 14:23, 14:32, 21:34, 21:43, 23:14, and 23:41. Example 2 a = 1 b = 4 c = 1 d = 4 return = 3 The distinct valid times are 11:44, 14:14, and 14:41. Repeated permutations of the equal digits do not add another time. Example 3 a = 8 b = 6 c = 7 d = 5 return = 0 No arrangement can put a valid digit in the tens place of the hour while also forming valid minutes. Constraints 0 <= a, b, c, d <= 9 Each parameter represents one decimal digit occurrence.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The input size kills any need for optimization. Four digits means at most 24 orderings, so brute force is the intended answer, not a shortcut. Generate every permutation of the four positions, build HH as the first two digits and MM as the last two, check HH <= 23 and MM <= 59, and drop the result into a set. The set handles deduplication for repeated digits, which is the whole pitfall. Don't try to count with factorial division, because validity filtering breaks that formula. Use itertools.permutations on indices or the digit list, since permutations treats equal values at different positions as distinct and the set collapses them. Encode each time as a tuple or as HH*100+MM. Return the set size. If the live clock is ticking and your mind goes blank, StealthCoder is the safety net that surfaces this exact approach on screen without the proctor seeing it.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Valid Times on a Digital Clock cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderThis OA pattern shows up on LeetCode as largest time for given digits. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Toptal's OA.
Toptal reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Valid Times on a Digital Clock FAQ
How hard is the Toptal Valid Times on a Digital Clock question really?+
Easy. With four digits there are only 24 orderings, so you enumerate them all and check validity. The difficulty is remembering duplicates. If you handle that with a set, you're done in under ten lines.
What's the trick to this problem?+
Brute-force all permutations and store valid times in a set. The set removes duplicate times caused by repeated digits, like 1,4,1,4 giving only 3 results. No formula, no pruning, just enumerate and filter.
Why can't I just divide by factorials for repeated digits?+
Because validity cuts the list unevenly. Dividing 24 by repeat counts only works if every arrangement is valid. Here many arrangements fail the hour or minute check, so count after filtering using a set instead.
How do I check validity correctly?+
Take the first two digits as hour: 10*p0+p1, must be 0 to 23. Take the last two as minutes: 10*p2+p3, must be 0 to 59. Leading zeroes are fine, so 00:00 counts. Test against example 3, which returns 0.
How do I prepare for this in 48 hours?+
Write the permutation plus set solution from memory once, then run all three examples by hand. Also practice the same pattern on similar small-input enumeration problems. It's a ten-minute problem if you recognize that the constraints permit brute force.