Digital Speedometer
InterviewTime limit1sMemory limit1024 MB
Apply hysteresis thresholds to a stream of sensor speeds so the integer display only changes when a reading crosses past the rising or falling threshold.
- Level
Medium4 of 10
- Topics
- Implementation, Simulation, Math, Array
- Solved
- No attempts yet
Problem
A digital speedometer shows a vehicle's speed as integer miles per hour. There are occasions when the sensed speed varies between two integer values, such as during cruise control. Using a single threshold to round between adjacent integers often makes the display toggle rapidly between the two integers, which is distracting to the driver.
Your team must implement a smoothing technique for the display using separate rising and falling thresholds ( and , , respectively). See Figure 1 for a graphical depiction of the Sample Input for use with the following rules.

Figure 1: Sensor reading from the Sample Input, with and .
Each sensed speed, , falls between two adjacent integers and , , where . When displaying the sensed speed as an integer:
- When falls between and , is displayed as .
- When falls between and , is displayed as .
- When falls between and , is displayed as if the most recent preceding value for outside of range is less than , and is displayed as if the most recent preceding value for outside of range is greater than .
- Any sensed speed, , must display as because any non-zero speed, no matter how small, must display as non-zero to indicate that the vehicle is in motion.
Input
The first line of input contains , the falling threshold. The second line of input contains , the rising threshold. The speed sensor reports in increments of mph. The thresholds are always set halfway between speed increments. All remaining lines until end-of-file are successive decimal speeds, , in miles per hour, one speed per line. The third line of input, which is the first measured speed, will always be . There are at most observed speeds in input.
Output
Output is the list of speeds, one speed per line, smoothed to integer values appropriate to and .