Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Find the first substring

Hardware interview practice

Find the first substring

EasyStringsSystemVerilog

Implement a function that returns the first index where sub appears inside s, or -1 when it is not present.

Try it in the question bankReason first. Then compare.

Keep this exact question selected while you check your answer and review the full solution.

Practice this question →
Reviewed example

Work through one case

Input
s = "silicon", sub = "lic"
Expected output
2

The first matching window starts at index 2; the later characters are not scanned after that first match is found.

What to cover

Requirements

  1. Return 0 for an empty substring.
  2. Return -1 when the substring is longer than the input.
  3. Return the first matching index, not every match.
asic.fyi · Learn silicon end to end.info@asic.fyi