Implement Substring Search
Updated
Problem statement
Given text and pattern, return the zero-based index of the first occurrence of pattern as a contiguous substring of text.
Return -1 when the pattern does not occur. An empty pattern returns 0, including when the text is empty.
Function
strStr(text: String, pattern: String) → intExamples
Example 1
text = "mississippi"pattern = "issip"return = 4Return the zero-based first matching position. The empty pattern occurs at index 0.
Example 2
text = "abc"pattern = "d"return = -1Return the zero-based first matching position. The empty pattern occurs at index 0.
Example 3
text = "abc"pattern = ""return = 0Return the zero-based first matching position. The empty pattern occurs at index 0.
Constraints
0 <= text.length, pattern.length <= 10^6- Both strings contain only lowercase English letters.
More IMC Trading Phone Screen questions
- Election District Budget Allocation AnalysisPHONE SCREEN · Seen Aug 2026
- Uniform Variables: Smaller Below Larger SquaredPHONE SCREEN · Seen Mar 2026
- Combination Sum with Unlimited ReusePHONE SCREEN · Seen Dec 2025
- Valid Range of a Three-Outcome Probability ParameterPHONE SCREEN · Seen Dec 2025
- Expected Fishing CatchPHONE SCREEN · Seen Dec 2025
- Count Routes Through a City GridPHONE SCREEN · Seen Nov 2024
- Expected City Arrival and Return DaysPHONE SCREEN · Seen Nov 2024
- Stationary City Population SharesPHONE SCREEN · Seen Nov 2024
- Maximum Falling Path SumPHONE SCREEN · Seen Nov 2024
- Trading Profit Percentage ChangePHONE SCREEN · Seen Feb 2024
Similar questions at other companies
- Classify Password StrengthHackerRank · OA · PHONE SCREEN · ONSITE INTERVIEWHackerRank interview questions
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Dasher Pay with Store Waits and Peak HoursDoorDash · PHONE SCREENDoorDash interview questions
- Fuzzy Word Lookup With One Misspelled LetterMeta · PHONE SCREENMeta interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Minimum Number of Frogs CroakingCerebras · PHONE SCREEN
- Adjacent Pair and Run RemovalWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions