Hardware interview practice
Build a deterministic memory-lane error histogram
A lab log contains the lane number for each observed error. Firmware must return all lane counts and the hottest lane. Implement the C histogram helper with transactional outputs.
Starting point
Question code
enum HistStatus { HIST_OK, HIST_BAD_ARG };
HistStatus lane_histogram(const uint8_t *events, size_t n, uint8_t lanes,
uint32_t *counts, uint8_t *hottest);Reviewed example
Work through one case
Input
events = {2, 1, 2, 3, 1, 2}, lanes = 4.Expected output
counts = {0, 2, 3, 1} and hottest = 2.Lane 2 occurs three times, more than every other lane, so it is the unique hottest lane.
What to cover
Requirements
- Require lanes in 1..32, counts and hottest nonnull, and events nonnull when n>0; reject n values that could overflow a uint32_t lane count.
- Every event must be below lanes. Validate the complete input before changing counts or *hottest.
- On success overwrite counts[0..lanes-1] with exact occurrence counts and set hottest to the lane with greatest count.
- Break a count tie in favor of the lowest lane number; n=0 therefore returns all-zero counts and hottest=0.
