Binary Watch (LeetCode 401)
A binary watch shows the time with 10 LEDs. Four LEDs on the top encode the hour (0 to 11) and six LEDs on the bottom encode the minute (0 to 59); each LED is one bit, with the lowest bit on the right.
Given turnedOn, the number of LEDs that are lit, return every time the watch could be showing, in any order. Write the hour without a leading zero ("1:00", never "01:00") and the minute with exactly two digits ("10:02", never "10:2").
Worked Examples
turnedOn = 1["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]turnedOn = 9[]11:59 lights 3 + 5), so 9 lit LEDs can't show a real time.⚖️Formal Constraints & Bounds
0 <= turnedOn <= 10
Core Insight
There are only 2^10 patterns of lit LEDs, and each is a number: test every mask, keep the ones with turnedOn bits, and decode the hour and minute fields with a shift and a mask, checking each field's range.
Real-World Scenario & Production Applications
Feature flags, permission sets and hardware status registers are all packed into integers, one bit per option. Enumerating every combination of a few flags (for example, to test each configuration) is a loop over range(1 << n), and decoding a register means shifting and masking its fields, then validating each one.
Step-by-Step Execution Trace Table
Input turnedOn = 2: only masks with exactly two 1 bits pass the popcount check (45 of the 1024).
mask (binary, hour bits | minute bits) | hour = mask >> 6 | minute = mask & 0b111111 | hour < 12 and minute < 60? | Action |
|---|---|---|---|---|
0000 | 000011 = 3 | 0 | 3 | Yes | append "0:03" |
0000 | 000101 = 5 | 0 | 5 | Yes | append "0:05" |
| ... | ||||
0000 | 110000 = 48 | 0 | 48 | Yes | append "0:48" |
0001 | 000001 = 65 | 1 | 1 | Yes | append "1:01" |
| ... | ||||
1100 | 000000 = 768 | 12 | 0 | No (hour 12) | skip: 4 bits can spell 12 |
| End | 44 times returned |
The Math, Step by Step
1. One number for every set of lit LEDs
The watch has 10 LEDs. Give each LED one bit of a 10-bit number, called a mask. Bit of the mask is 1 when LED is lit. The code puts the 4 hour LEDs in the top bits, 6 to 9, and the 6 minute LEDs in the low bits, 0 to 5.
A set of 10 items has subsets. Each mask from 0 to is exactly one of them, because each bit is either in or out. So the loop for mask in range(1 << 10) visits every possible set of lit LEDs once. 1 << 10 is , the number 1 shifted left by 10 bits.
2. Reading one mask
For one mask, the code needs three numbers:
| Number | Math | Code |
|---|---|---|
| LEDs lit | the number of 1 bits, called the popcount | bin(mask).count("1") |
| hour | the top 4 bits, | mask >> 6 |
| minute | the low 6 bits, | mask & 0b111111 |
mask >> 6 shifts the bits right by 6 places, so the 6 minute bits fall off and the hour bits remain. 0b111111 is 63, six 1 bits. mask & 0b111111 keeps only the low 6 bits, which is the minute.
For example, the mask 1011111011 in binary is 763. Its top 4 bits are 1011, which is 11. Its low 6 bits are 111011, which is 59. So it shows 11:59, and it lights LEDs.
3. Why the loop finds every time exactly once
Every time the watch can show has exactly one mask: the hour bits followed by the minute bits. Every mask gives exactly one hour and one minute. So walking all 1024 masks and keeping the ones that pass the checks lists every valid time once, with no repeats.
The count of masks with exactly lit LEDs is the binomial coefficient , the number of ways to choose bits out of 10. For that is 10 masks.
4. Masks that do not show a real time
4 bits can hold values up to 15, and 6 bits up to 63. But an hour must be at most 11, and a minute at most 59. So the code keeps a mask only when hour < 12 and minute < 60.
This check also explains Example 2. The hours 0 to 11 have at most 3 set bits (7 is 0111 and 11 is 1011). The minutes 0 to 59 have at most 5 set bits (for example 31 is 011111 and 59 is 111011). So a real time lights at most LEDs. Every mask with 9 or 10 lit LEDs fails the check, and the code returns an empty list.
5. Example 1 by hand
turnedOn = 1. The masks with exactly one 1 bit are the 10 powers of two, from to . The loop meets them in increasing order:
mask | mask in binary | hour = mask >> 6 | minute = mask & 0b111111 | Kept as |
|---|---|---|---|---|
| 1 | 0000000001 | 0 | 1 | "0:01" |
| 2 | 0000000010 | 0 | 2 | "0:02" |
| 4 | 0000000100 | 0 | 4 | "0:04" |
| 8 | 0000001000 | 0 | 8 | "0:08" |
| 16 | 0000010000 | 0 | 16 | "0:16" |
| 32 | 0000100000 | 0 | 32 | "0:32" |
| 64 | 0001000000 | 1 | 0 | "1:00" |
| 128 | 0010000000 | 2 | 0 | "2:00" |
| 256 | 0100000000 | 4 | 0 | "4:00" |
| 512 | 1000000000 | 8 | 0 | "8:00" |
All 10 pass the check hour < 12 and minute < 60. The format f"{hour}:{minute:02d}" writes the hour with no leading zero and the minute with two digits. The result is the expected output, in the same order.
6. Example 2 by hand
turnedOn = 9. There are masks with 9 lit LEDs. By part 4, none of them shows a real time. For example, the mask with only bit 0 off has hour 1111, which is 15, and fails hour < 12. The code returns [], the expected output.
7. From the math to the code
| Math | In readBinaryWatch |
|---|---|
| every subset of 10 LEDs, | for mask in range(1 << 10): |
popcount must equal turnedOn | if bin(mask).count("1") != turnedOn: followed by continue |
| hour = top 4 bits, minute = low 6 bits | hour, minute = mask >> 6, mask & 0b111111 |
| a real time: hour at most 11, minute at most 59 | if hour < 12 and minute < 60: |
| hour without a leading zero, minute with two digits | times.append(f"{hour}:{minute:02d}") |
The loop always visits masks, and counting the bits of each one takes at most 10 steps. This is the time with from the code's docstring.
| 1 | times = [] |
| 2 | for mask in <every 10-bit mask>: |
| 3 | if <its popcount is not turnedOn>: continue |
| 4 | hour, minute = <top 4 bits>, <low 6 bits> |
| 5 | if <hour and minute are real>: |
| 6 | <append the time as h:mm> |
| 7 | return times |
Conceptual Narrative
🧭 Conceptual Foundation & Pattern Intuition
When a problem has only a handful of on/off items, every subset is just a number. Give item i bit i: a number whose bit i is 1 means "item i is chosen". The subsets of n items are then exactly the numbers 0 .. 2^n - 1, so one for mask in range(1 << n) loop visits each subset once, with no recursion and no duplicates. Binary Watch is the purest case: the 10 LEDs are the items, and a lit LED is a 1 bit.
🏟️ The Analogy: A Row of Light Switches
Ten switches on a wall are a 10-digit binary number. Counting from 0 to 1023 flips them through every possible pattern exactly once. Reading the pattern back is just as easy: the left 4 switches spell the hour in binary, the right 6 spell the minute.
🔑 Why It Works
mask >> 6 drops the 6 minute bits and leaves the hour; mask & 0b111111 keeps only the minute bits. The range check matters: a field of 4 bits can hold 12 to 15, which are not hours, and 6 bits can hold 60 to 63, which are not minutes.
Skipping the range check:
hour < 12 and minute < 60must follow the split. ForturnedOn = 2the mask1100 000000decodes to hour 12, and forturnedOn = 4the minute bits111100decode to 60; both are impossible times.Wrong field split: the hour is the top 4 bits,
mask >> 6; the minute is the low 6 bits,mask & 0b111111(63). Usingmask & 0b1111ormask >> 4mixes hour and minute LEDs.Formatting: the minute needs two digits (
f"{minute:02d}") and the hour none extra ("1:00", not"01:00").Forgetting
turnedOn = 0: the empty pattern, mask 0, is the valid time"0:00";range(1 << 10)starts at 0, so it is included.
4-Phase Thought Process Model
Pattern Recognition Signals
The 10-second spot
"10 LEDs", each on or off, and "return all possible times": a small, fixed set of on/off items where you must try every combination. With n = 10 there are only 1024 subsets, so each subset can simply be a number: Bitmask Subsets.
Formulating the Predicate & Invariants
Turning intuition into a boolean rule
Bit i of mask is on exactly when LED i is lit, so range(1 << 10) visits each pattern once; a pattern is kept when bin(mask).count("1") == turnedOn and its decoded hour, minute = mask >> 6, mask & 0b111111 pass hour < 12 and minute < 60.
Silent Failure Traps & Edge Cases
Where confident candidates still lose points
This problem's traps are in Common Pitfalls above.
The 60-Second Interview Pitch
Say this out loud before you type a single line
I'd treat this as Bitmask Subsets. The watch has ten LEDs, so every pattern of lit LEDs is one number from 0 to 1023, with bit i on when LED i is lit. I loop over all 1024 masks and keep the ones whose popcount equals turnedOn. Then I decode: the top four bits, mask shifted right by six, are the hour, and the low six bits, mask and 63, are the minute. The trap is validation: four bits can spell up to 15 and six bits up to 63, so I keep a time only when the hour is below 12 and the minute below 60, and I format the minute with two digits. Each subset is one number, so every pattern is checked exactly once. It's 1024 masks with a 10-bit popcount each, a constant amount of work, and constant extra space beyond the answer.
Complexity & Mathematical Proof
O(B * 2^B), B = 10 LEDs
Look at the code: for mask in range(1 << 10) runs 2^B = 1024 times. Each iteration builds bin(mask) and counts its 1s in O(B), then does a shift, a mask and two comparisons in O(1). Total: O(B * 2^B) = about 10,000 basic steps, whatever turnedOn is.
O(1) extra, output not counted
Apart from the answer, the loop keeps mask, hour and minute and a short bin string of at most B + 2 characters: O(1) extra. The answer holds at most 720 times (12 hours x 60 minutes) and is not counted.
T = 2^B · O(B) = O(B · 2^B), B = 10
Derivation Progression
2^B = 1024 iterations
for mask in range(1 << 10) visits every LED pattern once.
O(B) per mask
bin(mask).count("1") looks at each of the B bits.
O(1) per kept mask
One shift, one AND, two comparisons and a short format.
O(B · 2^B)
About 10,000 steps: a constant, independent of turnedOn.
Variable Definitions
Number of LEDs (bits), 10 on this watch
One pattern of lit LEDs, a number from 0 to 2^B - 1
Memory Architecture & Bounds
O(1) Iterative, no recursion
O(1): mask, hour, minute, one bin string
At most 720 time strings (not counted)
Boundary Best / Worst Cases
: all masks are visited even when turnedOn is 0 or 10
Pointer Invariant Transition Progression
Senior SWE Deconstruction & Hardware Caveats
Triggers: "4 LEDs for the hour, 6 for the minute", "every time the watch could show". A fixed, tiny set of on/off items (10 bits) and an "every combination" question: Bitmask Subsets, one number per subset.
turnedOn and exactly bits, so masks: constant work. Bitmask enumeration stays practical up to about items ( masks).
Any packed register or flag word in production has the same trap: validate every field after masking, since reserved or out-of-range values can be encoded.
Binary Watch (LeetCode 401)
A binary watch shows the time with 10 LEDs. Four LEDs on the top encode the hour (0 to 11) and six LEDs on the bottom encode the minute (0 to 59); each LED is one bit, with the lowest bit on the right.
Given turnedOn, the number of LEDs that are lit, return every time the watch could be showing, in any order. Write the hour without a leading zero ("1:00", never "01:00") and the minute with exactly two digits ("10:02", never "10:2").
Worked Examples
turnedOn = 1["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]turnedOn = 9[]11:59 lights 3 + 5), so 9 lit LEDs can't show a real time.⚖️Formal Constraints & Bounds
0 <= turnedOn <= 10
Core Insight
There are only 2^10 patterns of lit LEDs, and each is a number: test every mask, keep the ones with turnedOn bits, and decode the hour and minute fields with a shift and a mask, checking each field's range.
Real-World Scenario & Production Applications
Feature flags, permission sets and hardware status registers are all packed into integers, one bit per option. Enumerating every combination of a few flags (for example, to test each configuration) is a loop over range(1 << n), and decoding a register means shifting and masking its fields, then validating each one.
Step-by-Step Execution Trace Table
Input turnedOn = 2: only masks with exactly two 1 bits pass the popcount check (45 of the 1024).
mask (binary, hour bits | minute bits) | hour = mask >> 6 | minute = mask & 0b111111 | hour < 12 and minute < 60? | Action |
|---|---|---|---|---|
0000 | 000011 = 3 | 0 | 3 | Yes | append "0:03" |
0000 | 000101 = 5 | 0 | 5 | Yes | append "0:05" |
| ... | ||||
0000 | 110000 = 48 | 0 | 48 | Yes | append "0:48" |
0001 | 000001 = 65 | 1 | 1 | Yes | append "1:01" |
| ... | ||||
1100 | 000000 = 768 | 12 | 0 | No (hour 12) | skip: 4 bits can spell 12 |
| End | 44 times returned |
The Math, Step by Step
1. One number for every set of lit LEDs
The watch has 10 LEDs. Give each LED one bit of a 10-bit number, called a mask. Bit of the mask is 1 when LED is lit. The code puts the 4 hour LEDs in the top bits, 6 to 9, and the 6 minute LEDs in the low bits, 0 to 5.
A set of 10 items has subsets. Each mask from 0 to is exactly one of them, because each bit is either in or out. So the loop for mask in range(1 << 10) visits every possible set of lit LEDs once. 1 << 10 is , the number 1 shifted left by 10 bits.
2. Reading one mask
For one mask, the code needs three numbers:
| Number | Math | Code |
|---|---|---|
| LEDs lit | the number of 1 bits, called the popcount | bin(mask).count("1") |
| hour | the top 4 bits, | mask >> 6 |
| minute | the low 6 bits, | mask & 0b111111 |
mask >> 6 shifts the bits right by 6 places, so the 6 minute bits fall off and the hour bits remain. 0b111111 is 63, six 1 bits. mask & 0b111111 keeps only the low 6 bits, which is the minute.
For example, the mask 1011111011 in binary is 763. Its top 4 bits are 1011, which is 11. Its low 6 bits are 111011, which is 59. So it shows 11:59, and it lights LEDs.
3. Why the loop finds every time exactly once
Every time the watch can show has exactly one mask: the hour bits followed by the minute bits. Every mask gives exactly one hour and one minute. So walking all 1024 masks and keeping the ones that pass the checks lists every valid time once, with no repeats.
The count of masks with exactly lit LEDs is the binomial coefficient , the number of ways to choose bits out of 10. For that is 10 masks.
4. Masks that do not show a real time
4 bits can hold values up to 15, and 6 bits up to 63. But an hour must be at most 11, and a minute at most 59. So the code keeps a mask only when hour < 12 and minute < 60.
This check also explains Example 2. The hours 0 to 11 have at most 3 set bits (7 is 0111 and 11 is 1011). The minutes 0 to 59 have at most 5 set bits (for example 31 is 011111 and 59 is 111011). So a real time lights at most LEDs. Every mask with 9 or 10 lit LEDs fails the check, and the code returns an empty list.
5. Example 1 by hand
turnedOn = 1. The masks with exactly one 1 bit are the 10 powers of two, from to . The loop meets them in increasing order:
mask | mask in binary | hour = mask >> 6 | minute = mask & 0b111111 | Kept as |
|---|---|---|---|---|
| 1 | 0000000001 | 0 | 1 | "0:01" |
| 2 | 0000000010 | 0 | 2 | "0:02" |
| 4 | 0000000100 | 0 | 4 | "0:04" |
| 8 | 0000001000 | 0 | 8 | "0:08" |
| 16 | 0000010000 | 0 | 16 | "0:16" |
| 32 | 0000100000 | 0 | 32 | "0:32" |
| 64 | 0001000000 | 1 | 0 | "1:00" |
| 128 | 0010000000 | 2 | 0 | "2:00" |
| 256 | 0100000000 | 4 | 0 | "4:00" |
| 512 | 1000000000 | 8 | 0 | "8:00" |
All 10 pass the check hour < 12 and minute < 60. The format f"{hour}:{minute:02d}" writes the hour with no leading zero and the minute with two digits. The result is the expected output, in the same order.
6. Example 2 by hand
turnedOn = 9. There are masks with 9 lit LEDs. By part 4, none of them shows a real time. For example, the mask with only bit 0 off has hour 1111, which is 15, and fails hour < 12. The code returns [], the expected output.
7. From the math to the code
| Math | In readBinaryWatch |
|---|---|
| every subset of 10 LEDs, | for mask in range(1 << 10): |
popcount must equal turnedOn | if bin(mask).count("1") != turnedOn: followed by continue |
| hour = top 4 bits, minute = low 6 bits | hour, minute = mask >> 6, mask & 0b111111 |
| a real time: hour at most 11, minute at most 59 | if hour < 12 and minute < 60: |
| hour without a leading zero, minute with two digits | times.append(f"{hour}:{minute:02d}") |
The loop always visits masks, and counting the bits of each one takes at most 10 steps. This is the time with from the code's docstring.
| 1 | times = [] |
| 2 | for mask in <every 10-bit mask>: |
| 3 | if <its popcount is not turnedOn>: continue |
| 4 | hour, minute = <top 4 bits>, <low 6 bits> |
| 5 | if <hour and minute are real>: |
| 6 | <append the time as h:mm> |
| 7 | return times |
Conceptual Narrative
🧭 Conceptual Foundation & Pattern Intuition
When a problem has only a handful of on/off items, every subset is just a number. Give item i bit i: a number whose bit i is 1 means "item i is chosen". The subsets of n items are then exactly the numbers 0 .. 2^n - 1, so one for mask in range(1 << n) loop visits each subset once, with no recursion and no duplicates. Binary Watch is the purest case: the 10 LEDs are the items, and a lit LED is a 1 bit.
🏟️ The Analogy: A Row of Light Switches
Ten switches on a wall are a 10-digit binary number. Counting from 0 to 1023 flips them through every possible pattern exactly once. Reading the pattern back is just as easy: the left 4 switches spell the hour in binary, the right 6 spell the minute.
🔑 Why It Works
mask >> 6 drops the 6 minute bits and leaves the hour; mask & 0b111111 keeps only the minute bits. The range check matters: a field of 4 bits can hold 12 to 15, which are not hours, and 6 bits can hold 60 to 63, which are not minutes.
Skipping the range check:
hour < 12 and minute < 60must follow the split. ForturnedOn = 2the mask1100 000000decodes to hour 12, and forturnedOn = 4the minute bits111100decode to 60; both are impossible times.Wrong field split: the hour is the top 4 bits,
mask >> 6; the minute is the low 6 bits,mask & 0b111111(63). Usingmask & 0b1111ormask >> 4mixes hour and minute LEDs.Formatting: the minute needs two digits (
f"{minute:02d}") and the hour none extra ("1:00", not"01:00").Forgetting
turnedOn = 0: the empty pattern, mask 0, is the valid time"0:00";range(1 << 10)starts at 0, so it is included.
4-Phase Thought Process Model
Pattern Recognition Signals
The 10-second spot
"10 LEDs", each on or off, and "return all possible times": a small, fixed set of on/off items where you must try every combination. With n = 10 there are only 1024 subsets, so each subset can simply be a number: Bitmask Subsets.
Formulating the Predicate & Invariants
Turning intuition into a boolean rule
Bit i of mask is on exactly when LED i is lit, so range(1 << 10) visits each pattern once; a pattern is kept when bin(mask).count("1") == turnedOn and its decoded hour, minute = mask >> 6, mask & 0b111111 pass hour < 12 and minute < 60.
Silent Failure Traps & Edge Cases
Where confident candidates still lose points
This problem's traps are in Common Pitfalls above.
The 60-Second Interview Pitch
Say this out loud before you type a single line
I'd treat this as Bitmask Subsets. The watch has ten LEDs, so every pattern of lit LEDs is one number from 0 to 1023, with bit i on when LED i is lit. I loop over all 1024 masks and keep the ones whose popcount equals turnedOn. Then I decode: the top four bits, mask shifted right by six, are the hour, and the low six bits, mask and 63, are the minute. The trap is validation: four bits can spell up to 15 and six bits up to 63, so I keep a time only when the hour is below 12 and the minute below 60, and I format the minute with two digits. Each subset is one number, so every pattern is checked exactly once. It's 1024 masks with a 10-bit popcount each, a constant amount of work, and constant extra space beyond the answer.
Complexity & Mathematical Proof
O(B * 2^B), B = 10 LEDs
Look at the code: for mask in range(1 << 10) runs 2^B = 1024 times. Each iteration builds bin(mask) and counts its 1s in O(B), then does a shift, a mask and two comparisons in O(1). Total: O(B * 2^B) = about 10,000 basic steps, whatever turnedOn is.
O(1) extra, output not counted
Apart from the answer, the loop keeps mask, hour and minute and a short bin string of at most B + 2 characters: O(1) extra. The answer holds at most 720 times (12 hours x 60 minutes) and is not counted.
T = 2^B · O(B) = O(B · 2^B), B = 10
Derivation Progression
2^B = 1024 iterations
for mask in range(1 << 10) visits every LED pattern once.
O(B) per mask
bin(mask).count("1") looks at each of the B bits.
O(1) per kept mask
One shift, one AND, two comparisons and a short format.
O(B · 2^B)
About 10,000 steps: a constant, independent of turnedOn.
Variable Definitions
Number of LEDs (bits), 10 on this watch
One pattern of lit LEDs, a number from 0 to 2^B - 1
Memory Architecture & Bounds
O(1) Iterative, no recursion
O(1): mask, hour, minute, one bin string
At most 720 time strings (not counted)
Boundary Best / Worst Cases
: all masks are visited even when turnedOn is 0 or 10
Pointer Invariant Transition Progression
Senior SWE Deconstruction & Hardware Caveats
Triggers: "4 LEDs for the hour, 6 for the minute", "every time the watch could show". A fixed, tiny set of on/off items (10 bits) and an "every combination" question: Bitmask Subsets, one number per subset.
turnedOn and exactly bits, so masks: constant work. Bitmask enumeration stays practical up to about items ( masks).
Any packed register or flag word in production has the same trap: validate every field after masking, since reserved or out-of-range values can be encoded.