This is an "Interactive" problem where you are placed in a house on a circular street. You don't know how many houses are on the street. You have two actions: move to the next house (clockwise) or "open/close" the door of the current house. The goal is to count the total number of houses. There is a limit on the number of moves/actions you can perform.
Google uses interactive problems to test a candidate's ability to think about state and consistency. Unlike static array problems, you must modify the environment to "mark" your progress. This specific problem tests your ability to create a "reset" point and measure the distance back to it, a technique used in many circular buffer and linked list scenarios.
The pattern used is Environmental Marking. To count the houses, you first need to ensure the street is in a known state. A common strategy is to walk along the street for a "sufficiently large" number of steps (given by the constraints) and close every door. Then, open the door of the house you are currently in. Move house by house, counting each step, until you encounter a house with an open door. That count is the number of houses.
Imagine a street with 3 houses.
A common mistake is forgetting to initialize the environment (closing all doors first). Without this, you might see an open door that was already open, giving an incorrect count. Another error is not understanding the circular nature—thinking the street has an "end" rather than wrapping around.
In interactive problems, always look for a way to "anchor" yourself. Since you can't see the whole data structure, modifying a piece of it (like opening a door or changing a value) becomes your way of detecting when you've returned to a specific point.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Read N Characters Given Read4 | Easy | Solve | |
| Guess the Majority in a Hidden Array | Medium | Solve | |
| Number of Equal Numbers Blocks | Medium | Solve | |
| Search in a Sorted Array of Unknown Size | Medium | Solve | |
| Find the Index of the Large Integer | Medium | Solve |