Count Houses in a Circular Street - Problem

Imagine you're a census taker walking through a mysterious circular neighborhood where you need to count the total number of houses. You start at a random house and can only interact with the street through a limited Street API.

The street forms a perfect circle - if you keep walking in one direction, you'll eventually return to where you started. Each house has a door that can be either open or closed, and you have the power to manipulate these doors as you move around.

Your Mission: Determine the exact number of houses on this circular street using the minimal number of API calls.

Available Street API Methods:

  • openDoor() - Open the current house's door
  • closeDoor() - Close the current house's door
  • isDoorOpen() - Check if current door is open (returns boolean)
  • moveRight() - Move to the next house clockwise
  • moveLeft() - Move to the next house counter-clockwise

You know there are at most k houses on the street, but the exact count is what you need to discover. The challenge is to do this efficiently while working with an interactive environment.

Input & Output

example_1.py โ€” Small Circle
$ Input: Street with 4 houses: [Open, Closed, Open, Open], k = 10
โ€บ Output: 4
๐Ÿ’ก Note: Starting at any house, we close it to mark our position, then walk around counting until we return to the closed door. The circle has exactly 4 houses.
example_2.py โ€” Single House
$ Input: Street with 1 house: [Open], k = 2
โ€บ Output: 1
๐Ÿ’ก Note: Edge case with only one house. We close it, move right (which brings us back to the same house since it's circular), and immediately find the closed door, so count = 1.
example_3.py โ€” All Doors Closed
$ Input: Street with 6 houses: [Closed, Closed, Closed, Closed, Closed, Closed], k = 10
โ€บ Output: 6
๐Ÿ’ก Note: Even when all doors are initially closed, our algorithm works because we're looking for the specific door we closed at the starting position after making a complete circle.

Visualization

Tap to expand
START (CLOSED)Count: 1Count: 2Count: 3Count: 7๐ŸŽฏ Total Houses: 8
Understanding the Visualization
1
Mark Your Starting Point
Close the door at your current position to create a unique landmark
2
Begin the Journey
Move to the next house and start counting (count = 1)
3
Walk and Count
Continue moving clockwise, incrementing counter for each house with an open door
4
Detect the Return
When you encounter a closed door, you've returned to your starting landmark
5
Complete the Census
Return the total count - you've counted every house exactly once
Key Takeaway
๐ŸŽฏ Key Insight: By creating our own landmark at the starting position, we can detect when we've completed exactly one full circle, ensuring we count every house exactly once.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass around the circle, visiting each house exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only need a counter variable, no additional data structures

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค number of houses โ‰ค k
  • k โ‰ค 103
  • Each house door is initially either open or closed
  • The street forms a perfect circle
  • You start at an arbitrary house position
Asked in
Google 42 Facebook 28 Amazon 35 Microsoft 19
38.2K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen