Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Measure the final UART command token

Hardware interview practice

Measure the final UART command token

EasyFirmware AlgorithmsSystemVerilog

Measure the last token in an explicitly sized UART command buffer after ignoring trailing ASCII space bytes.

Try it in the question bankReason first. Then compare.

Keep this exact question selected while you check your answer and review the full solution.

Practice this question →
Starting point

Question code

typedef enum { TOKEN_OK, TOKEN_EINVAL } token_rc_t;
token_rc_t last_token_len(const uint8_t *buf,
                          size_t len,
                          size_t *token_len);
// len <= 128; only 0x20 separates tokens.
Reviewed example

Work through one case

Input
buffer bytes="run test   ", explicit_length=11
Expected output
last_token_length=4 ("test")

Trailing ASCII spaces are skipped, then the scan stops at the separator before test without relying on a terminator.

What to cover

Requirements

  1. Treat only byte 0x20 as a separator; embedded zero and nonprinting bytes remain token data.
  2. Return zero for an empty or all-space input.
  3. Scan with unsigned indexes without decrementing below zero, calling strlen, allocating, or mutating the buffer.
  4. Reject a null output, length above 128, or null nonempty input without changing the caller's old length.
asic.fyi · Learn silicon end to end.info@asic.fyi