Interview Questions/Coding/Find the Solo Sensor Reading

Find the Solo Sensor Reading

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Find the Solo Sensor Reading

A sorted sensor log contains duplicate readings from paired devices. Exactly one reading was produced by a solo device and appears only once. Every other reading appears exactly twice, and equal readings are adjacent because the log is sorted. Find the solo reading. The returned value must be the reading itself, not its position. The input is guaranteed to follow the pairing rule, so exactly one answer exists. Input Format: You are given a sorted integer array readings. Output Format: Return the single value that appears exactly once.

Examples

Example 1
Input:
readings = [1, 1, 2, 3, 3, 4, 4, 8, 8]
Output:
2

Explanation: All readings are paired except value 2 at index 2. The pair pattern breaks there, so that value is returned.

Approach hint

Look at the index where each pair starts before and after the single value.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp