The Counting Words With a Given Prefix interview question asks you to find the number of strings in an array that start with a specific string called pref. You are given an array of strings words and a string pref. For each word in the array, you must check if it begins with the exact characters of the prefix.
Companies like Microsoft and Meta use the Counting Words With a Given Prefix coding problem as a basic screening question to evaluate a candidate's familiarity with string manipulation and library functions. It's an "Easy" difficulty problem that tests for speed, accuracy, and understanding of the "Prefix" concept in string matching. It also serves as a gateway to more complex problems like building a Trie (Prefix Tree).
This problem follows a simple Linear Scan / String Matching pattern.
words array.startsWith() in Java/JavaScript or slicing in Python) to check if the current word matches the prefix.Words: ["apple", "app", "apricot", "banana"], Prefix: "app"
While this problem is simple, mention that for a very large number of words and prefix queries, a Trie (Prefix Tree) data structure would be the optimal way to handle these requests in O(L) time, where L is the length of the prefix.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| String Matching in an Array | Easy | Solve | |
| Number of Senior Citizens | Easy | Solve | |
| Find Words Containing Character | Easy | Solve | |
| Maximum Number of Words Found in Sentences | Easy | Solve | |
| Repeated Substring Pattern | Easy | Solve |