Faulty Sensor - Problem

You're working as a quality assurance engineer for a high-tech laboratory where two identical sensors are used to collect critical experimental data simultaneously for redundancy and accuracy verification.

Each sensor produces an array of data points: sensor1[i] and sensor2[i] represent the i-th measurement from each sensor. However, these sensors have a known manufacturing defect that causes exactly one data point to be dropped during transmission.

When a data point is dropped:

  • All subsequent data points shift left by one position
  • The last position gets filled with a random value (guaranteed to be different from the dropped value)
  • For example: [1,2,3,4,5] with dropped value 3 becomes [1,2,4,5,7]

Your task: Determine which sensor (1 or 2) has the defect, or return -1 if it's impossible to determine or if both sensors are working correctly.

Input & Output

example_1.py โ€” Basic Case
$ Input: sensor1 = [2,3,4,5], sensor2 = [2,1,3,4]
โ€บ Output: 1
๐Ÿ’ก Note: Sensor1 is faulty. If we remove the first element (2) from sensor1, we get [3,4,5]. But sensor2 without the last element is [2,1,3]. These don't match. However, if we consider that sensor1 dropped element 1 that should have been at position 1, sensor1 becomes [2,1,4,5] which matches sensor2's first 3 elements [2,1,3]. Wait, let me reconsider... Actually, if sensor1 dropped the value that should be at position 1, sensor1 originally was [2,1,3,4,5], and after dropping 1, it became [2,3,4,5]. Sensor2 shows [2,1,3,4], so sensor1 is missing the 1.
example_2.py โ€” Sensor2 Faulty
$ Input: sensor1 = [2,1,3,4], sensor2 = [2,3,4,5]
โ€บ Output: 2
๐Ÿ’ก Note: Sensor2 is faulty. The original data should be [2,1,3,4,5]. Sensor2 dropped the element 1, causing [2,3,4,5] with the last element being a random value 5.
example_3.py โ€” No Defect Detectable
$ Input: sensor1 = [1,2,3], sensor2 = [1,2,3]
โ€บ Output: -1
๐Ÿ’ก Note: Both sensors show identical readings, so either both are working correctly or the defect cannot be determined from the given data.

Constraints

  • 1 โ‰ค sensor1.length, sensor2.length โ‰ค 105
  • 1 โ‰ค sensor1[i], sensor2[i] โ‰ค 109
  • sensor1.length == sensor2.length
  • At most one sensor has exactly one missing data point

Visualization

Tap to expand
Faulty Sensor Detection ProcessCamera A (Sensor1):F1F2F4F5?Camera B (Sensor2):F1F2F3F4F5Analysis: Mismatch detected at position 3Camera A shows F4, Camera B shows F3Hypothesis Testing:F1F2F3?F4F5Insert missing F3โœ“ Solution Found!Camera A dropped frame F3, causing subsequent frames to shiftResult: Sensor 1 is faulty
Understanding the Visualization
1
Synchronize Playback
Start comparing both recordings frame by frame from the beginning
2
Detect Anomaly
Stop when you find the first frame that doesn't match between cameras
3
Test Hypotheses
Check if skipping one frame from either camera resolves all remaining differences
Key Takeaway
๐ŸŽฏ Key Insight: Once we find where the recordings diverge, we only need to test if one camera missed a single frame at that exact point
Asked in
Google 28 Amazon 22 Meta 18 Microsoft 15
42.3K Views
Medium Frequency
~15 min Avg. Time
1.5K 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