Ferozeh dot Com



Expert advice on tech interviews !

about ferozeh


Ferozeh !

Question

Difficulty Level: 5

  • Say you have disc which is half black and half white. You have sensors which you can place under the disc. First give a solution using any number of sensors to determine if the disc is rotating clockwise or anti-clockwise. Next optimize the solution using the fewest possible number of sensors. The sensors returns 1 if the black portion of the disc is over it and 0 if the white portion of the disc is over it.

Solution

The problem can be solved ver easily with three or four sensors. The optimized answer has only two sensors. Let's describe the optimzed solution as understanding it will make the problem a cakewalk with a greater number of sensors. Look at the disc below:

Assume that we have placed two sensors A and B at the location shown on the disc. Furthermore assume that the clock rotates anticlockwise. Initially the reading on the sensors will be:



Sensor Reading

A

1

B

1

Next assume the disc rotates by a quarter giving us the following situation:

Sensor Reading

A

1

B

0

For the next quarter both sensors read 0 since the situation becomes:

Sensor Reading

A

0

B

0

Now the trick becomes amply clear here. Trace the entir path for the four quarters which is

Anticlockwise
SensorQuarter 1Quarter 2Quarter 3 Quarter 4
A1100
B1 0 0 1

Do the same for clockwise

Clockwise
SensorQuarter 1Quarter 2Quarter 3 Quarter 4
A1001
B1 1 0 0

I hope you see the trick now. When moving clockwise when both sensor A and B turn black i.e. output 1 the next change must be black for A and white for B i.e. 1 on A and 0 on B. However this is opposite of what will happen when moving clockwise i.e. after a 1 1 the next change should be white on A and black on B i.e. A should give 0 and B should give 1. Thus this is what we would test for while the disc runs in order to determine if it is spinning clockwise or anticlockwise.

Custom Search
Next