On a recent job interview, I was asked the following question:
A conveyer belt at a factory carries wooden planks. Planks are placed with spaces between them (length of spaces can vary). Each plank is either shorter or longer than 1 metre. Two sensors, L and R, are placed on the conveyer belt, exactly 1 metre apart. If the sensor value is 1, it means a wooden plank is present, 0 otherwise. Each sensor sends a signal only when its state is changed.
Describe how implement a signal handler, the aim is to count how many short and long wooden planks passed at the belt.
Void sighandler (char sensor)
My approach: I described a finite state machine. We hold a pair of the current sensor state (L,R). Distinguish two cases:
Short plank passes: (0,0) -> (1,0) -> (0,0) -> (0,1) -> (0,0)
Long plank passes: (0,0) -> (1,0) -> (1,1) -> (0,1) -> (0,0)
I suggested that when we reach state (1,0): if the next state is (0,0), increment the short counter by 1. If the next state is (1,1), increment the long counter by 1.
The interviewer told me that the solution is correct, but only under the assumption that the space between every two consecutive planks is at least 1 metre.
How to solve for the general case? Tried to find the question online without success.
Edit 1: if we look only at one sensor, the number of bit flips of this sensor from 0 to 1, is equal to the total amont of planks (short + long).