Google | Onsite | Banglore | May 2022 | Count House in the Circle
Anonymous User
9251

Problem Statement:

There are some house in a cicular street. something like in the below picture

image

each house is connected to its neighbours, on each house you are allowed to do the below operations:

  • void openDoor()
  • void closeDoor()
  • boolean isDoorOpen()
  • House moveRight()
  • House moveLeft()

Suppose Initially, it is given that all the House doors are closed. and you are standing at one random house. and you have to count the total number of houses in the Circle. (by moving to the right or left) . how will you count the number of houses?

My Approach

int count = 1;
// open the door of first house only
openDoor();
moveRight();
// keep moving to right until we found that house of which we openned the door initially
while(!isDoorOpen()){
	count++;
	moveRight();
}
return count;

Followup Question:
Now Suppose the House doors are randomly closed and open initialliy and you are standing at one random house. you have given a max number, the total_number_of_houses <= max. now how will you count the total number of houses

Full Interview Experience Here
https://leetcode.com/discuss/interview-experience/2072074/google-software-engineer-l3-banglore-may-2022-reject

Comments (13)