Skip to question
ASIC.FYI
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
ASIC.FYI/Interview questions/Merge inclusive overlapping intervals

Q218·Free·SystemVerilog

Merge inclusive overlapping intervals

Difficulty
Medium
Topic
Arrays
Language
SV
Interview prompt

Question

Given a queue of inclusive [start, end] intervals, merge every overlap and return the merged intervals sorted by start.

Starting point

Question code

typedef struct {
  int unsigned start_val;
  int unsigned end_val;
} interval_t;

function automatic interval_t[$]
merge_intervals(interval_t intervals[$]);
Reviewed example

Trace one case

Input
intervals = [[1,2], [2,4], [6,7], [8,9]]
Expected output
[[1,4], [6,7], [8,9]]

Inclusive ranges sharing endpoint 2 merge, while [6,7] and [8,9] remain separate because adjacency alone is not overlap.

What to cover

Requirements

  1. Assume every input interval satisfies start_val <= end_val.
  2. Merge intervals that share an endpoint, such as [1,2] and [2,4].
  3. Do not merge merely adjacent integer ranges such as [1,2] and [3,4].
  4. Return an empty queue for empty input.
Exact question handoffPractice Q218

Solve it in the question bank, keep your progress, and reveal the reviewed solution when your access allows.

Open in question bank →
Solution accessA Free account unlocks the complete reviewed solution.
Continue learning

SystemVerilog

  • Arrays
  • SystemVerilog
  • Intervals
  • Sorting
Firmware interview questions →
Continue practicing

Related questions

Q229 · Data StructuresMaintain the top-K most frequent stream values→Q289 · ArraysRemove duplicates while preserving order→Q232 · ArraysForward-looking sliding window maximum→
ASIC.FYI · Learn silicon end to end.info@asic.fyi