↓Skip to main content
  1. LeetCode/

LeetCode 1826: Faulty Sensor

·2 mins· ·
LeetCode Easy Two-Pointers Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-04-05

  • Submissions: Wrong Answer ×2 → Accepted

Approach
#

A faulty sensor drops one value, shifts the rest left by one, and fills the last slot with a random value (guaranteed not to equal the dropped value). At most one sensor is faulty.

Find the first i with sensor1[i] != sensor2[i]:

  • If sensor2 is faulty, it dropped sensor1[i], so sensor2[i:-1] should equal sensor1[i+1:].
  • If sensor1 is faulty, likewise sensor1[i:-1] should equal sensor2[i+1:].

Only one explanation fitting identifies the sensor; if both or neither fit, it cannot be determined, so return -1. No mismatch at all also returns -1.

My Attempts
#

  1. Attempt 1 compared only the next element: sensor1[i + 1] == sensor2[i] meant sensor2 was faulty.
  2. Attempt 2 compared the whole remaining tail but still did not handle both explanations fitting.

Both fail on sensor1 = [3, 3, 2, 3], sensor2 = [3, 2, 3, 2]. Sensor2 could have dropped the second 3 (with 2 appended), and sensor1 could have dropped sensor2’s first 2 (with 3 appended); both fit, so the answer is -1, but both versions returned 2.

Solution
#

class Solution:
    def badSensor(self, sensor1: List[int], sensor2: List[int]) -> int:
        n = len(sensor1)
        for i in range(n - 1):
            if sensor1[i] != sensor2[i]:
                sensor2_bad = sensor1[i + 1:] == sensor2[i:-1]
                sensor1_bad = sensor2[i + 1:] == sensor1[i:-1]
                if sensor1_bad == sensor2_bad:
                    return -1
                return 1 if sensor1_bad else 2
        return -1

The loop stops at n - 2: if only the last position differs, it is just the random fill value, and the faulty sensor cannot be identified.

Complexity
#

Time O(n), space O(n) (slices copy).

Mistakes to Avoid
#

  • Concluding from the next element alone.
  • Not handling the case where both explanations fit.

Interview Explanation
#

I find the first mismatch i and test each hypothesis: if sensor2 is faulty, its data from i should equal sensor1’s data from i + 1, ignoring the last slot, and symmetrically for sensor1. Exactly one fitting identifies the sensor; if both or neither fit, I return -1. Time O(n).

Related

LeetCode 2200: Find All K-Distant Indices in an Array
·2 mins
LeetCode Easy Two-Pointers Array
LeetCode 2200 note: extend each key k positions both ways, and keep a pointer to how far output has gone so overlapping ranges are not emitted twice
LeetCode 1089: Duplicate Zeros
·2 mins
LeetCode Easy Array Two-Pointers
LeetCode 1089 note: count the zeros to know each value’s shift, then move from back to front, writing the extra 0 when copying a zero
LeetCode 977: Squares of a Sorted Array
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 977 note: the largest squares are at the two ends, so compare the ends with two pointers and fill the result from the back
LeetCode 905: Sort Array By Parity
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 905 note: a write pointer swaps even numbers to the front, just like move zeroes
LeetCode 922: Sort Array By Parity II
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 922 note: pointers i over even slots and j over odd slots, each stepping by two; when slot i holds an odd number, move j to a misplaced even and swap
LeetCode 821: Shortest Distance to a Character
·2 mins
LeetCode Easy Array Two-Pointers String
LeetCode 821 note: one left-to-right pass for the distance to the nearest c on the left, then a right-to-left pass taking the minimum