401. Binary Watch
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find all possible times on a conceptual binary watch, given the total number of "on" LEDs. The watch has two sections: 4 LEDs for the hour (values 0-11) and 6 LEDs for the minute (values 0-59). The value of the hour or minute is the sum of the powers of 2 for the LEDs that are on. This is equivalent to finding numbers whose binary representation has a certain number of set bits (also known as Hamming weight).
Solution
Binary Watch in Python
class Solution:
def readBinaryWatch(self, turnedOn: int) -> List[str]:
result = []
# Iterate through all possible hours (0-11)
for h in range(12):
# Iterate through all possible minutes (0-59)
for m in range(60):
# Count the number of set bits (1s) in the binary representation.
# In Python 3.10+, h.bit_count() is available.
# bin(h).count('1') is compatible with older versions.
if bin(h).count('1') + bin(m).count('1') == turnedOn:
# If the bit counts sum up to turnedOn, format the time string.
# f-string formatting `{m:02d}` ensures the minute is two digits with a leading zero if needed.
result.append(f"{h}:{m:02d}")
return result
Binary Watch in Java
class Solution {
public List<String> readBinaryWatch(int turnedOn) {
List<String> result = new ArrayList<>();
// Iterate through all possible hours (0-11)
for (int h = 0; h < 12; h++) {
// Iterate through all possible minutes (0-59)
for (int m = 0; m < 60; m++) {
// Count the number of set bits (1s) using the built-in Integer.bitCount method.
if (Integer.bitCount(h) + Integer.bitCount(m) == turnedOn) {
// If the total number of set bits matches turnedOn, format and add the time.
// String.format("%d:%02d", h, m) handles minute formatting (e.g., 5 -> "05").
result.add(String.format("%d:%02d", h, m));
}
}
}
return result;
}
}
Binary Watch in C++
class Solution {
public:
vector<string> readBinaryWatch(int turnedOn) {
vector<string> result;
// Iterate through all possible hours (0-11)
for (int h = 0; h < 12; ++h) {
// Iterate through all possible minutes (0-59)
for (int m = 0; m < 60; ++m) {
// Count the number of set bits (1s) in the binary representation of hour and minute.
// __builtin_popcount is a GCC/Clang extension that's very efficient.
if (__builtin_popcount(h) + __builtin_popcount(m) == turnedOn) {
// If the total number of set bits matches turnedOn, format and add the time to the result.
string minute_str = to_string(m);
if (m < 10) {
minute_str = "0" + minute_str;
}
result.push_back(to_string(h) + ":" + minute_str);
}
}
}
return result;
}
};
Binary Watch in TypeScript
function readBinaryWatch(turnedOn: number): string[] {
const result: string[] = [];
// Helper function to count set bits in a number's binary representation.
const countSetBits = (n: number): number => {
let count = 0;
while (n > 0) {
n &= (n - 1); // This clears the least significant bit
count++;
}
return count;
};
// Iterate through all possible hours (0-11)
for (let h = 0; h < 12; h++) {
// Iterate through all possible minutes (0-59)
for (let m = 0; m < 60; m++) {
// If the total number of set bits for hour and minute equals turnedOn
if (countSetBits(h) + countSetBits(m) === turnedOn) {
// Format the time string and add it to the result.
// `padStart` ensures the minute is always two digits.
const minuteStr = m.toString().padStart(2, '0');
result.push(`${h}:${minuteStr}`);
}
}
}
return result;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.