Detect Near-Duplicate Sensor Readings Within a Time and Value Window
Last updated: November 29, 2026
Quick Overview
An Azure IoT pipeline receives integer readings in order. Return True if two readings at most max_gap positions apart differ in value by at most tolerance.
Microsoft
November 29, 20260
0
0 solved
An Azure IoT pipeline receives integer readings in order. Return True if two readings at most max_gap positions apart differ in value by at most tolerance.
This tests ordered-window reasoning: a candidate needs a structure that answers 'is any value in the last k close to x' quickly. Microsoft interviewers accept a sorted container solution and are impressed by the bucket approach with an O(n) bound.
What the Interviewer Expects
- Start from the O(n x k) brute force
- Maintain only the last max_gap readings
- Use a sorted structure for floor and ceiling queries, or buckets of width tolerance + 1
- Argue that each bucket holds at most one reading or we already returned True
- Handle negative values and tolerance 0
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the pair of indices, not just True.
- Readings arrive as an infinite stream with timestamps instead of positions.
- How does the sorted container version compare, and what would you use in Java or C#?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
readings: list of ints, possibly negative.max_gap >= 0,tolerance >= 0.- Output: True if some pair i != j has |i - j| <= max_gap and |r[i] - r[j]| <= tolerance....
High-Level Design
Keep the last max_gap readings in a structure that can find the closest value to x. In C# or Java, a SortedSet or TreeSet gives floor and ceiling in O(log k), for O(n log k) overall.
The bucket tri...