Software Debounce Filter
You receive a stream of digital samples (0 or 1) from a mechanical switch.
Return 1 only if the last 5 samples are all 1, otherwise return 0.
This simulates software debouncing of a noisy input.
Input: An integer array of samples and its length n (where n ≥ 5). Output: 1 if the last 5 samples are all 1, else 0.
Constraints
- Do not allocate dynamic memory.
- Only inspect the last 5 samples.
- O(1) time.
1int debounce(int *samples, int n) {2 // n is always >= 53 return 0;4}