|
Expert advice on tech interviews ! |
| Ferozeh !
|
SolutionThe 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: ![]()
Next assume the disc rotates by a quarter giving us the following situation:
For the next quarter both sensors read 0 since the situation becomes:
Now the trick becomes amply clear here. Trace the entir path for the four quarters which is
Do the same for clockwise
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.
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||