1
$\begingroup$

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:

  1. Short plank passes: (0,0) -> (1,0) -> (0,0) -> (0,1) -> (0,0)

  2. 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).

New contributor
SleepingOwl132 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
1
  • $\begingroup$ Holding additional variables is permitted of course. Can you explain your solution, and why it is correct? $\endgroup$ Commented Dec 11 at 11:44

1 Answer 1

1
$\begingroup$

If you number each plank from $1$ to $n$, you can keep track of which plank each sensor last observed by keeping two counters $L_c$ and $R_c$ (for left counter and right counter, respectively).

When the left sensor, $L$ turns on, you increment $L_c$ (because $L$ is now looking at a new plank). When $L$ turns off, you increment $n$, the total number of planks observed.

When the right sensor, $R$ turns on, you increment $R_c$, and check if $L_c = R_c > n$. If it is so, then you increment $n_{\text{long}}$.

You can now compute $n_{\text{short}} = n - n_{\text{long}}$.

The reason it is correct is because $X_c$ is how many times sensor $X$ has been turned on, and indicates which plank it last saw. If, in addition, $L_c = R_c > n$ it means that the last plank they witnessed was plank $L_c$ and since $n$ hasn't been incremented yet, $L$ is currently observing it, hence $R$ and $L$ are observing the same plank, hence it is a long plank.


In code:

from dataclasses import dataclass

@dataclass
class Conveyor:
    L_c: int = 0
    R_c: int = 0
    n: int = 0
    n_long: int = 0

    def signal(self, sensor, status):
        if sensor == "L":
            if status == "ON":
                self.L_c += 1
            else:
                self.n += 1
        else:
            if status == "ON":
                self.R_c += 1
                if self.L_c == self.R_c > self.n:
                    self.n_long += 1

    @property
    def short(self):
        return self.n - self.n_long

    @property
    def long(self):
        return self.n_long
conveyor = Conveyor()
conveyor.signal("L", "ON")
conveyor.signal("L", "OFF")
conveyor.signal("R", "ON")
conveyor.signal("R", "OFF")
conveyor.signal("L", "ON")
conveyor.signal("R", "ON")
conveyor.signal("L", "OFF")
conveyor.signal("R", "OFF")
print(conveyor.short)
print(conveyor.long)
$\endgroup$
1
  • 1
    $\begingroup$ Very clear explanation. Thanks! $\endgroup$ Commented Dec 11 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.