Leetcode #2728: Count Houses in a Circular Street
In this guide, we solve Leetcode #2728 Count Houses in a Circular Street in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
You are given an object street of class Street that represents a circular street and a positive integer k which represents a maximum bound for the number of houses in that street (in other words, the number of houses is less than or equal to k). Houses' doors could be open or closed initially.
Quick Facts
- Difficulty: Easy
- Premium: Yes
- Tags: Array, Interactive
Intuition
The constraints allow a direct scan that keeps only the essential state.
By translating the requirements into a clean loop, the logic stays easy to reason about.
Approach
Iterate through the data once, updating the state needed to compute the answer.
Return the final state after the traversal is complete.
Steps:
- Parse the input.
- Iterate and update state.
- Return the computed answer.
Example
Input: street = [0,0,0,0], k = 10
Output: 4
Explanation: There are 4 houses, and all their doors are closed.
The number of houses is less than k, which is 10.
Python Solution
# Definition for a street.
# class Street:
# def openDoor(self):
# pass
# def closeDoor(self):
# pass
# def isDoorOpen(self):
# pass
# def moveRight(self):
# pass
# def moveLeft(self):
# pass
class Solution:
def houseCount(self, street: Optional["Street"], k: int) -> int:
for _ in range(k):
street.openDoor()
street.moveLeft()
ans = 0
while street.isDoorOpen():
street.closeDoor()
street.moveLeft()
ans += 1
return ans
Complexity
The time complexity is O(n). The space complexity is O(1) to O(n).
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.