The "Check If a Word Occurs As a Prefix of Any Word in a Sentence interview question" involves searching for a specific string pattern within a larger text. You are given a sentence (a string of words separated by single spaces) and a searchWord. You need to find the 1-indexed position of the first word in the sentence that starts with searchWord. If no such word exists, return -1.
Companies like Microsoft and Bloomberg use the "Check If a Word Occurs As a Prefix coding problem" to assess basic string parsing and searching skills. It tests whether you can split a sentence into components, handle 1-based indexing, and use language-specific string methods (like startsWith or find) correctly. It’s a core "String interview pattern" task.
This problem follows the String Tokenization and Prefix Matching pattern.
searchWord and if the beginning of the word matches searchWord.Sentence: "i love eating burger", searchWord: "burg"
["i", "love", "eating", "burger"]searchWord is anywhere inside the word (like "er" in "burger") instead of specifically at the start.Familiarize yourself with the string manipulation library of your preferred language. Using built-in methods like split() and startsWith() makes your code cleaner and more readable during the interview.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find the Index of the First Occurrence in a String | Easy | Solve | |
| Repeated Substring Pattern | Easy | Solve | |
| Reverse String II | Easy | Solve | |
| Merge Strings Alternately | Easy | Solve | |
| Reverse Words in a String III | Easy | Solve |