Hardware interview practice
Count errors in a build report
A build tool returns a byte buffer containing newline-separated log records; the buffer is not NUL-terminated. Write the C++ parser that counts records beginning with ERROR: and WARNING:.
Starting point
Question code
struct Counts { size_t errors, warnings; };
int count_log(const char *p, size_t n, Counts *out);Reviewed example
Work through one case
Input
A 20-byte buffer containing "ERROR: a\nWARNING: b\n".Expected output
Return 0 with errors=1 and warnings=1.Each newline-delimited record begins with one exact uppercase prefix, so the bounded parser increments each counter once without needing a NUL terminator.
What to cover
Requirements
- Require out; p may be NULL only when n=0. Invalid arguments return -1 and preserve *out.
- A record starts at byte zero or immediately after '\n' and ends before the next '\n' or at n.
- Count a record only when its first bytes exactly match uppercase ERROR: or WARNING:; leading spaces do not match.
- Treat '\r' as an ordinary byte except that a trailing '\r' before '\n' does not affect prefix matching; write *out once and return 0.
