Count valid 24-hour times from four digits
I was asked this
No reports yet. Sign in to say where you were asked it.
Write a function count_valid_times(a, b, c, d) that takes four digits (each an integer from 0 to 9).
Arrange all four digits as HH:MM on a 24-hour clock, using each given digit exactly once. A time is valid when the hour is from 00 to 23 and the minute is from 00 to 59. Leading zeros are allowed.
Return the number of distinct valid times. If the digits repeat, two different arrangements may show the same time; count that time once.
Constraints
0 <= a, b, c, d <= 9
Example 1
Input: a = 1, b = 8, c = 3, d = 2
Output: 6
The valid times are 12:38, 13:28, 18:23, 18:32, 21:38 and 23:18.
Example 2
Input: a = 2, b = 3, c = 3, d = 2
Output: 3
The valid times are 22:33, 23:23 and 23:32. Several orderings of the digits produce each of these, but each time counts once.
Example 3
Input: a = 6, b = 2, c = 4, d = 7
Output: 0
Any hour that starts with 2 would be 24 or more, and there is no 0 or 1 to start an hour with.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.
