SERVO INTERVIEW LAB
01 / 05
Question 3 · understand first

Poll a live servo at approximately 50 Hz

THE JOB

A servo exposes read_position_rad() . Print its measured velocity around 50 times per second until interrupted. Calls occasionally take a few milliseconds.

inputsoutputunitsinvalid data
SERVO INTERVIEW LAB
02 / 05
Mental model

Make the mechanism intuitive.

01

Use a monotonic clock for intervals. Separate one loop iteration from the infinite loop so it remains testable.

→
02

Repeatedly sleeping 20 ms after work makes the period “work time + 20 ms.” Schedule against an advancing deadline instead.

SERVO INTERVIEW LAB
03 / 05
Derive the answer

One decision at a time.

STEP 1

Tradeoff to discuss: ordinary Python cannot promise hard real-time behavior.

→
STEP 2

This is adequate for a practical measurement/telemetry loop, but a control/safety loop may need hardware timestamps, a real-time process, bounds, and explicit missed-deadline metrics.

→
STEP 3

Validate before updating state, then prove the nominal and boundary cases with deterministic tests.

SERVO INTERVIEW LAB
04 / 05
Python walkthrough

Make the contract visible in code.

import time
from collections.abc import Callable

def monitor_servo(
    read_position_rad: Callable[[], float],
    rate_hz: float = 50.0,
) -> None:
    if rate_hz <= 0:
        raise ValueError("rate_hz must be positive")
    period = 1.0 / rate_hz
    previous: tuple[float, float] | None = None
    deadline = time.monotonic()

    try:
        while True:
KEY INVARIANT

This is adequate for a practical measurement/telemetry loop, but a control/safety loop may need hardware timestamps, a real-time process, bounds, and explicit missed-deadline metrics.

validate → calculate → update
SERVO INTERVIEW LAB
05 / 05
Prove + communicate

Finish like an engineer.

normal example
boundary case
invalid input
INTERVIEW CLOSE

Tradeoff to discuss: ordinary Python cannot promise hard real-time behavior. This is adequate for a practical measurement/telemetry loop, but a control/safety loop may need hardware timestamps, a real-time process, bounds, and explicit missed-deadline metrics.

invariant · policy · complexity · next step