← Back to list

Understanding the Binary Watch Problem in Python (Step-by-Step Guide)

While practicing coding problems, I recently worked on an interesting problem called Binary Watch, which helped me better understand binary…

Amisha · 2026-02-17 11:36 · 3 claps · 1.7 min read
#data-science #python-for-data-science #leetcode
Open on Medium ↗
Wiki topics: ML · Machine Learning 💻 · Programming 🔬 · Science · General

Understanding the Binary Watch Problem in Python (Step-by-Step Guide)

While practicing coding problems, I recently worked on an interesting problem called Binary Watch, which helped me better understand binary numbers, bit counting, and brute-force logic in Python.

Let me walk you through the intuition and solution in a simple way.

What is a Binary Watch?

A binary watch displays time using LEDs instead of regular digits. The watch has:

4 LEDs for hours (values: 8, 4, 2, 1)

6 LEDs for minutes (values: 32, 16, 8, 4, 2, 1)

Each LED can be:

ON → represented by 1

OFF → represented by 0

So, the time is represented in binary format.

Problem Statement

Given a number turnedOn, representing how many LEDs are ON, we need to return all possible valid times.

Example:

turnedOn = 1

Only one LED is ON, so possible outputs include:

0:01, 0:02, 1:00, 2:00, ...

Key Idea Behind the Solution

Instead of manually simulating LEDs, we can:

  1. Try all valid times.
  2. Convert hours and minutes into binary.
  3. Count how many 1s appear.
  4. Keep times where total LEDs ON equals turnedOn.

Since:

Hours range: 0–11
Minutes range: 0–59

We only check:

12 × 60 = 720 possibilities

which is small and efficient.

Python Solution

from typing import List
class Solution:
    def readBinaryWatch(self, turnedOn: int) -> List[str]:
        result = []
        for hour in range(12):
            for minute in range(60):
                leds_on = bin(hour).count("1") + bin(minute).count("1")
                if leds_on == turnedOn:
                    result.append(f"{hour}:{minute:02d}")
        return result

Important Python Trick Used

Binary conversion

bin(5)

Output

'0b101'

Count number of 1s

bin(5).count("1")

Output:

2

This directly tells us how many LEDs are ON.

Example Walkthrough

If:

hour = 3 → binary = 11 → two LEDs ON
minute = 1 → binary = 1 → one LED ON

Total LEDs ON:

2 + 1 = 3

time 3:01 is valid when turnedOn = 3.

What I Learned

From this problem, I reinforced:

Binary number understanding

Looping through possibilities

Filtering using conditions

Python string and formatting tricks

Sometimes the simplest brute-force logic works efficiently when input size is small.

Final Thoughts

Problems like this are excellent for strengthening fundamentals. Instead of worrying about optimization early, focus on clarity and correctness first.

Small problems build strong foundations.

Thanks for reading! Follow my journey as I continue exploring Python, Data Science, and problem solving.


메타데이터
post_id
d8655dfe549b
slug
understanding-the-binary-watch-problem-in-python-step-by-step-guide-d8655dfe549b
url
https://medium.com/@chaudharia1/understanding-the-binary-watch-problem-in-python-step-by-step-guide-d8655dfe549b
canonical_url
https://medium.com/@chaudharia1/understanding-the-binary-watch-problem-in-python-step-by-step-guide-d8655dfe549b
author_url
https://medium.com/@chaudharia1
status
ok
fetched_at
2026-06-09 15:37:30