Q037FreeFirmware
Find a calibration insertion position
Interview prompt
Question
Complete find_insert_pos to return the first index in a nondecreasing uint16_t calibration table whose value is at least key, or count when no entry qualifies. A nonnull table points to at least count readable elements that remain unchanged for the call. A nonnull position points to a writable size_t object separate from the table. Null pointers are handled as specified; arbitrary dangling pointers are outside the interface contract.
Candidate starting point
Implementation scaffold
#include <stddef.h>
#include <stdint.h>
typedef enum { SEARCH_OK, SEARCH_EINVAL } search_rc_t;
search_rc_t find_insert_pos(const uint16_t *table, size_t count,
uint16_t key, size_t *position) {
// TODO: validate first, search [lo,hi), and commit only the final boundary.
}
Reviewed example
Trace one case
Input
table=[10,20,20,40]; key=20Expected output
position=1The half-open lower-bound search returns the first duplicate whose value is not less than the key.
What to cover
Requirements
- Use a half-open [lo, hi) search and a midpoint that cannot overflow.
- Resolve a duplicate run to its first index and never read table[count].
- Allow an empty table with a null table pointer and return position zero.
- Reject an invalid output, count above 64, or null nonempty table without changing the saved output.
