Longest Sensor Signal Segment Within an Amplitude Tolerance
Last updated: November 29, 2026
Quick Overview
Given a sampled signal as a list of numbers and a tolerance tol, return the length and index range of the longest contiguous segment whose maximum minus minimum is at most tol.
MathWorks
November 29, 20260
0
0 solved
Given a sampled signal as a list of numbers and a tolerance tol, return the length and index range of the longest contiguous segment whose maximum minus minimum is at most tol.
MathWorks engineers work on Signal Processing Toolbox and Simulink data inspection, where finding stable plateaus in sensor data is a common preprocessing step. The interviewer is checking whether you can maintain a running max and min over a sliding window in amortized O(1) using monotonic deques rather than rescanning the window.
What the Interviewer Expects
- Identifies the sliding window pattern and argues why the window can only grow from the left boundary forward
- Uses two monotonic deques (decreasing for max, increasing for min) holding indices
- Shrinks the left edge correctly and evicts stale deque heads
- Proves amortized O(n) since each index enters and leaves each deque once
- Returns the index range, not only the length, and defines tie behavior
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
- How would you adapt this to a streaming signal where you must report a stable segment as soon as it reaches a minimum length?
- What if the tolerance is relative, such as max at most 1.05 times min, for strictly positive signals?
- How would you vectorize a fixed-width version of this in MATLAB using movmax and movmin?
- How do NaN samples change the design?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list signal of floats, tol >= 0. Output: (length, (start, end)) of the longest contiguous segment with max - min <= tol, inclusive indices. On ties keep the earliest segment. An empty s...
High-Level Design
Use a two-pointer window [left, right]. As right advances, push it onto a deque of indices whose values are decreasing (front is the window max) and another whose values are increasing (front is t...