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: Element Drop DetectionCorrect Data:1234Sensor1 (Good):1234Drop element 2:1234Sensor2 (Faulty):1347ShiftedShiftedRandomResult: Sensor 2 is faultyDetection: Compare arrays and find which has valid drop pattern
Understanding the Visualization
1
Input
Two sensor arrays with potential defect in one
2
Defect Pattern
Dropped element causes left shift and random end value
3
Detection
Compare and identify which sensor is faulty
Key Takeaway
🎯 Key Insight: A faulty sensor creates a cascade pattern - one missing element causes all subsequent elements to shift left
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