#2728
Easy Algorithms Count houses in a circular street
Array Interactive
86.2% acceptance
Mar 31, 2026
60
12
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.
Initially, you are standing in front of a door to a house on this street. Your task is to count the number of houses in the street.
The class Street contains the following functions which may help you:
void openDoor(): Open the door of the house you are in front of.
void closeDoor(): Close the door of the house you are in front of.
boolean isDoorOpen(): Returns true if the door of the current house is open and false otherwise.
void moveRight(): Move to the right house.
void moveLeft(): Move to the left house.
Return ans which represents the number of houses on this street.
Solution
Rust
Time O(n)
Space O(1)
/**
* Definition for a street.
* impl Street {
* pub fn new(doors: Vec<i32>) -> Self {}
* pub fn open_door(&mut self) {}
* pub fn close_door(&mut self) {}
* pub fn is_door_open(&self) -> bool {}
* pub fn move_right(&mut self) {}
* pub fn move_left(&mut self) {}
* }
*/
impl Solution {
pub fn house_count(street: Street, k: i32) -> i32 {
let mut street = street;
for _ in 0..k {
street.close_door();
street.move_right();
}
street.open_door();
street.move_right();
let mut count = 1;
while !street.is_door_open() {
street.move_right();
count += 1;
}
count
}
}