Faulty Sensor - Problem

An experiment is being conducted in a lab. To ensure accuracy, there are two sensors collecting data simultaneously.

You are given two arrays sensor1 and sensor2, where sensor1[i] and sensor2[i] are the ith data points collected by the two sensors.

However, this type of sensor has a chance of being defective, which causes exactly one data point to be dropped. After the data is dropped, all the data points to the right of the dropped data are shifted one place to the left, and the last data point is replaced with some random value.

It is guaranteed that this random value will not be equal to the dropped value. For example, if the correct data is [1,2,3,4,5] and 3 is dropped, the sensor could return [1,2,4,5,7] (the last position can be any value, not just 7).

We know that there is a defect in at most one of the sensors. Return the sensor number (1 or 2) with the defect. If there is no defect in either sensor or if it is impossible to determine the defective sensor, return -1.

Input & Output

Example 1 — Sensor1 Missing Element
$ Input: sensor1 = [1,3,4,5], sensor2 = [1,2,3,4]
Output: 1
💡 Note: Sensor1 is missing element 2. If we remove element 3 from position 1 in sensor1, we get [1,4,5], but this doesn't match sensor2's first 3 elements [1,2,3]. However, sensor1 appears to have dropped element 2, causing all subsequent elements to shift left and the last position to be filled with value 5.
Example 2 — No Defect
$ Input: sensor1 = [1,2,3,4], sensor2 = [1,2,3,4]
Output: -1
💡 Note: Both sensors have identical readings, so there's no defect in either sensor.
Example 3 — Sensor2 Missing Element
$ Input: sensor1 = [1,2,3,4], sensor2 = [1,3,4,7]
Output: 2
💡 Note: Sensor2 is missing element 2. The correct sequence should be [1,2,3,4], but sensor2 shows [1,3,4,7] where 2 was dropped and 7 was added at the end.

Constraints

  • 2 ≤ sensor1.length, sensor2.length ≤ 1000
  • sensor1.length == sensor2.length
  • 1 ≤ sensor1[i], sensor2[i] ≤ 106
  • At most one sensor is defective

Visualization

Tap to expand
Faulty Sensor - Optimal Solution INPUT sensor1 1 3 4 5 [0] [1] [2] [3] sensor2 1 2 3 4 [0] [1] [2] [3] Problem: One sensor may have dropped a data point. Data shifts left after drop, last value becomes random. Find the faulty sensor! ALGORITHM STEPS 1 Find First Mismatch Compare arrays element by element until difference found 1 3 1 2 Mismatch at i=1! 2 Check Shift Patterns Test if s1[i] matches s2[i+1] or s2[i] matches s1[i+1] 3 Compare Rest s1: 3,4,5 vs s2[1:]: 2,3,4 s2: 2,3,4 vs s1[1:]: 3,4,5 s1[1:] = [3,4,5] s2[2:] = [3,4] + X Match! 4 Determine Fault sensor1 dropped value "2" so sensor1 is faulty FINAL RESULT Correct Data (reconstructed): 1 2 3 4 FAULTY SENSOR 1 OUTPUT 1 sensor1 dropped "2" at index 1 [1,2,3,4] shifted to [1,3,4,5] "5" is the random end value Key Insight: When a value is dropped, all subsequent values shift left by one position. To find the faulty sensor, locate the first mismatch, then check which sensor's remaining data (after the mismatch) aligns with the other sensor's data shifted by one. The sensor whose data can be explained by a dropped value is the faulty one. Return -1 if ambiguous. TutorialsPoint - Faulty Sensor | Optimal Solution
Asked in
Google 15 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~15 min Avg. Time
342 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen