Faulty Sensor

There are two sensors collecting data simultaneously. Each sensor gives us an array of integers. For the most part, these two arrays are identical because the sensors are supposed to be collecting the same data.

However, there’s a catch. One or both of the sensors might have a defect. If a sensor is defective, it drops exactly one data point (integer value). This causes a ripple effect where every data point to the right of the dropped point is shifted one place to the left, effectively overwriting the dropped value. The last position is then filled with some random value which is not equal to the dropped value.

The task is to identify the sensor with the defect. If sensor1 is defective, return 1. If sensor2 is defective, return 2. If there are no defects or if it is impossible to determine which one is defective, return -1.

For example, consider these readings:

sensor1 = [2, 3, 4, 5] sensor2 = [2, 1, 3, 4]

Here, the second value (index 1) in sensor2 is different from the corresponding value in sensor1. Now, check if the remaining values in sensor1 starting from index 1 are equal to the values in sensor2 starting from index 2, i.e., [3, 4, 5] == [3, 4]. It’s not the case here. Therefore, sensor1 is defective.

It’s not always possible to identify the defective sensor. In some cases, both sensors might appear defective or it’s impossible to tell which one is defective based on the data. In such cases, the function should return -1.

Here is a table for the first example:

sensor1 = [2,3,4,5] sensor2 = [2,1,3,4]

IndexSensor 1 ReadingSensor 2 ReadingNotes
022Same, continue checking
131Different, possible defect point
243Sensor 1’s data matches sensor 2’s next data. Sensor 1 is likely the defective one.
354Sensor 1’s data doesn’t match sensor 2’s data (from previous index). This confirms sensor 1 as defective.

Based on this table, we can see that sensor1 is likely the defective one because it seems like it dropped a data point at index 1 (with the reading of 3) and replaced the last data point with a random value (5). We confirm this because the remaining values from sensor1 starting from the defect point match the remaining values in sensor2 shifted one index to the right. Thus, the answer is 1, indicating sensor1 is defective.

1
2
3
4
5
6
7
8
9
class Solution:
    def badSensor(self, sensor1: List[int], sensor2: List[int]) -> int:
        i = 0
        sz = len(sensor1)
        while i < sz and sensor1[i] == sensor2[i]:
            i += 1
        while i + 1 < sz and sensor1[i] == sensor2[i + 1] and sensor1[i + 1] == sensor2[i]:
            i += 1
        return -1 if i >= sz - 1 else 1 if sensor1[i] == sensor2[i + 1] else 2

The function badSensor in the Solution class compares elements in both lists. If it finds a discrepancy and is unable to decide which sensor is defective, it returns -1. If it can determine which sensor is defective, it returns the sensor number (1 or 2).

The provided Python function badSensor in the Solution class is designed to determine which one of the two sensors is faulty given their respective readings. The sensors are deemed faulty if they deviate from each other at any point. If it cannot be determined which sensor is faulty, the function returns -1. If sensor1 is faulty, the function returns 1. If sensor2 is faulty, the function returns 2.

Here’s how the function works:

  1. The function starts by initializing i to 0, which will serve as the index for iterating through the sensor readings.

  2. The function then enters the first while loop, which continues as long as the readings from both sensors are the same. This loop is used to skip the initial readings where both sensors agree.

  3. If a discrepancy between the sensors is found, the function enters the second while loop. This loop continues as long as the i-th reading of sensor1 equals the (i+1)-th reading of sensor2, and the (i+1)-th reading of sensor1 equals the i-th reading of sensor2. This loop is used to check if the sensors are off by one reading, which would indicate that one of the sensors started malfunctioning at index i.

  4. After exiting the second while loop, if i is greater than or equal to sz-1, it means the sensors have been consistent throughout all their readings, or they deviated only in the last reading. In this case, it cannot be determined which sensor is faulty, so the function returns -1.

  5. If i is less than sz-1, it means one of the sensors deviated at index i. If the i-th reading of sensor1 equals the (i+1)-th reading of sensor2, it indicates that sensor1 is faulty, so the function returns 1. Otherwise, sensor2 is deemed faulty and the function returns 2.

This solution is based on the observation that if a sensor is faulty, the readings from that sensor will deviate from the other sensor’s readings at some index, and all subsequent readings from the faulty sensor will be shifted by one position.