Software Debounce Filter

Medium C Robustness Asked at Continental
Solve this problem

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.
Starter codeC
1int debounce(int *samples, int n) {2    // n is always >= 53    return 0;4}