Hardware interview practice
Bounded byte-string palindrome check
A firmware diagnostic receives a byte buffer and length; the bytes are not NUL-terminated. Write the C function that reports whether the buffer reads the same forward and backward.
Starting point
Question code
bool is_palindrome(const uint8_t *p, size_t n);Reviewed example
Work through one case
Input
p=NULL and n=0.Expected output
Return true.The empty byte sequence is a palindrome, and the function handles that case before dereferencing p or computing n-1.
What to cover
Requirements
- Return true for lengths zero or one; p may be NULL only when n is zero.
- For n greater than zero, return false if p is NULL.
- Compare byte values exactly, including zero bytes, and do not modify the buffer.
- Run in O(n) time with O(1) extra storage and avoid index underflow for n=0.
