In a stream pipeline, which clock-driven operator suits a dial turned in bursts, and which suits a continuously drifting reading?
answer
- does the source ever go quiet?
- one per burst versus one per tick
- debounce waits for silence
- sampling ignores how many arrived
- a never-quiet source starves a debounce
basics
~20 sA quiet-period debounce fits the dial: it emits one settled value once the turning stops. Periodic sampling fits the drifting reading: it emits whatever is current on each tick, because that source never falls quiet.
solid answer
~40 sBoth reduce the rate of a sequence, but they ask different questions. A quiet-period debounce is data-driven: every arrival restarts a timer, and a value is released only after the source has been silent for the whole window, so a burst of turns produces exactly one output - the position the occupant settled on. Periodic sampling is clock-driven: on each tick of a fixed interval it emits the current value and forgets the rest. So the choice follows the event rate. A bursty source with real gaps between bursts suits debouncing, and you get one meaningful value per burst. A source that drifts without ever pausing suits sampling, because a debounce whose window never elapses emits nothing at all. Both preserve order and both discard values.
code
pseudocode · 22 linesfunction debounce(source, quietWindow):
out = new_stream()
pending = NONE
timer = NONE
on source value v:
pending = v
if timer is not NONE:
cancel(timer)
timer = schedule_after(quietWindow, release)
function release():
out.emit(pending)
pending = NONE
timer = NONE
on source end:
if timer is not NONE:
cancel(timer) # this variant drops a pending value
out.end()
return outgo deeper
Recall the one-line difference: a debounce waits for the source to go quiet, sampling emits on a fixed tick no matter what the source is doing.
Explain the mechanics. The debounce timer restarts on every arrival; the sampling tick does not. Derive the output rate and the worst-case wait of each from that one fact.
Choose from measured evidence, not taste. Show the gap distribution of the real source, place the window in its valley if there is one, and state the staleness the consumer is being promised.
Frame the choice as a contract. Bursty and continuous feeds want different reductions, so the durable decision is the staleness bound you publish, not the operator you happen to pick this quarter.
## Two operators, two different questions about the clock A pipeline stage that reduces the rate of a sequence has to decide **when** to emit. Two clock-driven stages dominate this leaf, and they take opposite approaches. **Quiet-period debouncing** is *data-driven*. The stage keeps the most recent value and restarts a fixed-length timer on every arrival. The held value is released only when that timer expires, which can only happen if no newer value arrived during the whole window. In the common *trailing* form, a burst of twenty values separated by short gaps produces exactly one output: the value that ended the burst. (A *leading* variant emits the first value of a burst and suppresses the rest; when an interviewer says debounce without qualifying, the trailing form is what they mean.) **Periodic sampling** is *clock-driven*. It ignores the arrival pattern completely: on each tick of a fixed interval it emits the value that is current at that instant and forgets everything else. Twenty arrivals inside one interval produce one output. An interval with no arrival at all produces either a repeat of the previous value or nothing - implementations differ on that point, so say which behaviour you are relying on rather than assuming it. Neither stage slows the producer down. Both discard values to lower the output rate; making a fast producer actually go slower is demand signalled upstream, which is a different mechanism. ## What each one promises | | quiet-period debounce | periodic sampling | |---|---|---| | what triggers an emission | a gap of at least the window with no new value | the next tick of a fixed interval | | output rate | one per burst; the gap between outputs is unbounded | at most one per interval, steady | | latency after the final change | the window length, **if** the source falls quiet | up to one full interval | | what it discards | every value of a burst except the settling one | everything between two ticks | | fails badly when | the source never falls quiet for a whole window | the value moves and reverts between two ticks | | output timing depends on | the data | the clock alone | ## The question to ask about the source Before choosing, look at the distribution of gaps between consecutive values: - **Bimodal with a valley** - short gaps inside a burst, long gaps between bursts. Put the window in the valley and a debounce gives you one value per burst, which is usually the value that matters. - **Roughly continuous** - values arrive at a steady rate with no pause worth the name. There is no valley to put a window in, so debouncing either emits nothing (window above the gap) or passes almost everything through (window below it). Sample instead. - **Rare and isolated** - a handful of changes an hour. Neither reduction earns its place; the raw sequence is already slow. ## The dial and the reading The occupant turns the dial in bursts and then lets go. Only the position they settled on is worth acting on, and the burst ends with a real silence, so a debounce whose window sits just above the gap between individual clicks emits one value per adjustment. The temperature reading, by contrast, drifts continuously; it is never silent. Its consumer does not need every reading, only a recent one, and a fixed interval gives exactly that: a bounded staleness, a predictable output rate, and no dependence on how busy the source happens to be. ## Where each one hurts - A debounce adds **unbounded latency** in the worst case: nothing promises the source will fall quiet, and until it does, the held value sits there. That is not a slow stage, it is a starved one. - Sampling **aliases**: a change that appears and reverts between two ticks is invisible downstream, and a value that arrives just after a tick waits nearly a full interval. If the consumer must see every distinct state the source passed through, no clock-driven reduction is the right tool. - Both are lossy by design. Auditing, billing and anything where a skipped value is a missing fact should not sit behind either of them. ## A stage that only shifts time Delaying every emission by a fixed amount is often confused with these two and is neither. A delay preserves the count and the spacing of the sequence and moves only its phase: twenty values in, twenty values out, each later by the same amount. It aligns a sequence with something else; it reduces nothing.
- How does delaying every emission by a fixed amount differ from a quiet-period debounce of the same duration?A delay shifts the whole sequence: the same number of values come out, spaced as they went in, each later by the fixed amount. A debounce changes the sequence: all but the settling value of each burst are dropped, and consecutive outputs are separated by at least the window. One moves values in time, the other removes them.
- What is the worst-case wait between the occupant's last turn and the value reaching the consumer?For a debounce it is the window length, but only if the source then stays quiet; if turning resumes before the window elapses, the wait extends indefinitely. For sampling it is one interval: a value that lands just after a tick waits almost a whole interval for the next one.
- The dial is nudged once and returns to its old position before the next tick - what does periodic sampling deliver?Nothing that reflects the nudge. Both samples see the same position, so the excursion is invisible downstream. That is aliasing, and it is the price of a clock-driven reduction: it reports state at instants, not the path between them.
A debounce is a photographer waiting for the subject to stop fidgeting before pressing the shutter; sampling is a security camera taking one frame a second whatever is happening.
saying these in an interview costs you the question
- Assumes a quiet-period debounce always emits something eventually
- Thinks periodic sampling forwards every value seen during the interval
- Treats debounce and sampling as interchangeable regardless of event rate
- Confuses delaying every value with dropping all but the last of a burst
- Believes either stage makes the producer slow down