Skip to question
SystemVerilogDesignVerificationFirmwareArchitectureASIC Interview Questions→
/Interview questions/Measure the final UART command token

Q054·Free·Firmware

Measure the final UART command token

Difficulty
Easy
Topic
Firmware Algorithms
Language
C
Interview prompt

Question

Measure the last token in an explicitly sized UART command buffer after ignoring trailing ASCII space bytes. Nonnull pointers refer to valid storage for their stated extents. The token_len output object must not overlap the input byte span, and the input is immutable during the call. The null and empty-input cases below still apply.

Sixteen-byte UART buffer containing STATUS followed by two spaces and unused capacity
The example token STATUS occupies indices 0 through 5; the following two ASCII spaces are trimmed. Scan within the explicit input length, ignoring unused capacity.
Candidate starting point

Implementation scaffold

#include <stddef.h>
#include <stdint.h>

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) {
  // Implement here: validate arguments, find the final bounded token,
  // and publish its length only after complete success.
}
Reviewed example

Trace 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. Keep embedded zero and nonprinting bytes as token data.
  2. Return zero for an empty or all-space input.
  3. Use unsigned indexes. Do not decrement below zero, call strlen, allocate memory, or change the buffer.
  4. Reject the call if token_len is null, len is above 128, or buf is null while len is nonzero. Leave the caller’s previous output length unchanged on rejection.
Exact question handoffPractice Q054

Solve it in the question bank, keep your progress, and reveal the reviewed solution when your access allows.

Open in question bank →
Solution accessEach time you open this Solution, one Practice Credit is used; it is not permanently unlocked. Premium Solution content also uses one credit per opening.
Continue learning

Firmware Guide

Review algorithms, data structures, fixed-memory reasoning, concurrency, and silicon bring-up.

  • Firmware Algorithms
  • Buffer
  • Backward scan
  • Bounds
Firmware Guide →
Continue practicing

Related questions

Q679 · FirmwareCalculate UART 8N1 Frame Time and Payload RateEasyP→Q985 · Protocols and InterfacesCalculate UART Baud and Payload RatesEasyP→
ASIC.FYI · Learn silicon end to end.info@asic.fyi