SERVO INTERVIEW LAB
01 / 05
Question 6 · understand first

Measure velocity from a large CSV log

THE JOB

Read timestamp_ns,position_deg from a large CSV and write timestamp_ns,velocity_rad_s . Include useful errors for malformed rows.

inputsoutputunitsinvalid data
SERVO INTERVIEW LAB
02 / 05
Mental model

Make the mechanism intuitive.

01

Stream rows with csv.DictReader . Convert nanoseconds to seconds and degrees to radians before differentiation.

→
02

For an interview, state the choice; do not silently discard data.

SERVO INTERVIEW LAB
03 / 05
Derive the answer

One decision at a time.

STEP 1

Policy question: fail fast versus skip-and-count malformed rows depends on the use case.

→
STEP 2

For an interview, state the choice; do not silently discard data.

→
STEP 3

Streaming gives O(1) auxiliary memory.

SERVO INTERVIEW LAB
04 / 05
Python walkthrough

Make the contract visible in code.

import csv
import math
from typing import TextIO

def convert_log(source: TextIO, destination: TextIO) -> None:
    reader = csv.DictReader(source)
    required = {"timestamp_ns", "position_deg"}
    if not required.issubset(reader.fieldnames or []):
        raise ValueError(f"required columns: {sorted(required)}")

    writer = csv.DictWriter(
        destination, fieldnames=["timestamp_ns", "velocity_rad_s"]
    )
    writer.writeheader()
    previous: tuple[int, float] | None = None
KEY INVARIANT

For an interview, state the choice; do not silently discard data.

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

Finish like an engineer.

normal example
boundary case
invalid input
INTERVIEW CLOSE

Policy question: fail fast versus skip-and-count malformed rows depends on the use case. For an interview, state the choice; do not silently discard data. Streaming gives O(1) auxiliary memory.

invariant · policy · complexity · next step