Magicsheet logo

Faulty Sensor

Easy
25%
Updated 8/1/2025

Asked by 1 Company

Faulty Sensor

What is this problem about?

The Faulty Sensor interview question involves two arrays representing data from two sensors. One of the sensors is faulty. The fault occurs at exactly one index i: the sensor drops the value at i and then records all subsequent values shifted by one position. The last value recorded by the faulty sensor is then filled by an arbitrary value (or ignored). You need to determine which sensor is faulty (1, 2, or both/unknown).

Why is this asked in interviews?

Meta asks this Array interview pattern to test your attention to detail and ability to identify shifts in linear data. It's an "Easy" difficulty problem that evaluates whether you can handle index comparisons and find the point of divergence between two sequences. It requires a clean implementation of "what if" logic.

Algorithmic pattern used

This problem uses a Two Pointers / Linear Scan approach.

  1. Find the first index i where sensor1[i] != sensor2[i].
  2. If no such index exists (up to the last element), return -1 (unknown).
  3. Check if sensor 1 could be faulty: This means the rest of sensor1 from i should match sensor2 from i+1.
  4. Check if sensor 2 could be faulty: This means the rest of sensor2 from i should match sensor1 from i+1.
  5. Return the result based on which condition (or both) is true.

Example explanation

Sensor 1: [2, 3, 4, 5], Sensor 2: [2, 4, 5, 6]

  1. Index 0: 2 == 2.
  2. Index 1: 3 != 4. Point of divergence is 1.
  3. Test if Sensor 1 dropped its value at index 1:
    • sensor1[1] should match sensor2[2]? 3 vs 5. No.
  4. Test if Sensor 2 dropped its value at index 1:
    • sensor2[1..2] ([4, 5]) matches sensor1[2..3] ([4, 5])? Yes. Result: Sensor 2 is faulty.

Common mistakes candidates make

  • Ignoring the last element: Not realizing that the fault can't be at the very last index because you wouldn't be able to tell which one dropped a value.
  • Incorrect shift check: Comparing indices incorrectly (e.g., comparing sensor1[i] with sensor2[i] instead of i+1).
  • Loop Bounds: Going out of bounds when checking the shifted segments.

Interview preparation tip

Whenever you need to check if one array is a "shifted" version of another, focus on the first point of difference. Once found, the relationship for the remainder of the arrays is fixed.

Similar Questions