Skip to question
SystemVerilogDesignVerificationFirmwareArchitectureASIC Interview Questions→
/Interview questions/Find lower and upper bounds

Q159·Free·SystemVerilog

Find lower and upper bounds

Difficulty
Medium
Topic
Arrays
Language
SV
Interview prompt

Question

For a sorted integer array, return the first index with value >= target and the first index with value > target.

Candidate starting point

Implementation scaffold

function automatic int lower_bound_int(
  const ref int values[],
  input int target
);
    // TODO: implement this body.
  endfunction

function automatic int upper_bound_int(
  const ref int values[],
  input int target
);
    // TODO: implement this body.
  endfunction
Reviewed example

Trace one case

Input
values = [1, 2, 2, 2, 5], target = 2
Expected output
lower_bound = 1, upper_bound = 4

The half-open matching range [1,4) contains all three occurrences of 2.

What to cover

Requirements

  1. Return values.size() when the requested bound does not exist.
  2. Handle duplicate targets correctly.
  3. Use half-open search intervals.
Exact question handoffPractice Q159

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.

  • Arrays
  • Binary search
  • Sorted array
  • Duplicates
Firmware Guide →
Continue practicing

Related questions

Q182 · Data StructuresUse lower-bound insertion for top-K largestMedium→Q037 · Firmware AlgorithmsFind a calibration insertion positionEasy→
ASIC.FYI · Learn silicon end to end.info@asic.fyi