Skip to question
SystemVerilogDesignVerificationFirmwareArchitectureASIC Interview Questions→
/Interview questions/Find a calibration insertion position

Q037·Free·Firmware

Find a calibration insertion position

Difficulty
Easy
Topic
Firmware Algorithms
Language
C
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=20
Expected output
position=1

The half-open lower-bound search returns the first duplicate whose value is not less than the key.

What to cover

Requirements

  1. Use a half-open [lo, hi) search and a midpoint that cannot overflow.
  2. Resolve a duplicate run to its first index and never read table[count].
  3. Allow an empty table with a null table pointer and return position zero.
  4. Reject an invalid output, count above 64, or null nonempty table without changing the saved output.
Exact question handoffPractice Q037

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
  • Binary search
  • Lower bound
  • Bounds
Firmware Guide →
Continue practicing

Related questions

Q182 · Data StructuresUse lower-bound insertion for top-K largestMedium→Q218 · RTL DesignSearch a synchronous calibration ROMHardP→Q684 · ArraysSort only a subrangeEasyP→Q555 · FirmwareSort Signed Integers In Place with Insertion SortEasyP→
ASIC.FYI · Learn silicon end to end.info@asic.fyi