Skip to the selected question
ASIC.FYI

ASIC Question Bank

1,000+ hardware interview questions
DescriptionQ619
Page ↗
Q619FWASIC interview problem

Find the first substring

TechniquesStringNested scan
DifficultyEasy
TopicStrings
LanguageSystemVerilog
Requirements3 checkpoints
01

Problem

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

Example input and output

Use this case to check your interpretation
Input
s = "silicon", sub = "lic"
Output
2
Explanation

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

02

Requirements (3)

  • Return 0 for an empty substring.
  • Return -1 when the substring is longer than the input.
  • Return the first matching index, not every match.