Find the Words for Each Prefix
Updated
Problem statement
You are given an array of different words words and an array of queries prefixes.
For each prefix, find every word that starts with it. A word starts with a prefix when its first letters are exactly the prefix; a word also starts with itself.
Return a list with one entry per prefix, in the same order as prefixes. Each entry lists the matching words in alphabetical order, or is empty when no word matches. If a prefix appears more than once, answer it each time.
Function
wordsForPrefixes(words: String[], prefixes: String[]) → List<List<String>>Examples
Example 1
words = ["sign","signal","seal","sigma","send"]prefixes = ["sig","se","x"]return = [["sigma","sign","signal"],["seal","send"],[]]Words starting with sig: sign, signal and sigma, listed alphabetically as sigma, sign, signal. Words starting with se: seal and send. No word starts with x, so that list is empty.
Example 2
words = ["doc","docs","dock"]prefixes = ["doc","docks"]return = [["doc","dock","docs"],[]]All three words start with doc, including doc itself. No word starts with docks, because docks is longer than every word that begins with dock.
Example 3
words = ["a","ab","b"]prefixes = ["a","b","a"]return = [["a","ab"],["b"],["a","ab"]]The prefix a appears twice, so its list appears twice: a and ab. The prefix b matches only b.
Constraints
1 <= words.length <= 30001 <= prefixes.length <= 10001 <= words[i].length, prefixes[j].length <= 10- Every word and prefix contains only lowercase English letters.
- All words are different.
- The returned lists hold at most
1000words in total.
More DocuSign Phone Screen questions
- Symmetric Binary TreePHONE SCREEN · Seen Oct 2026
- Union Area of Two RectanglesPHONE SCREEN · Seen Oct 2026
- Execute Cursor-Based Text Editor OperationsPHONE SCREEN · Seen Sep 2026
- Minimize Array Sum After K Halving OperationsPHONE SCREEN · Seen Sep 2026
- Fizz and Buzz Words From 1 to nPHONE SCREEN · Seen Sep 2026
- Two Positions That Add Up to a TargetPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Rain Kept Between WallsPHONE SCREEN · Seen Mar 2026
- Exact Palindrome CheckPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Reverse the Characters of a TextPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Rebuild a Trip From Flight TicketsPHONE SCREEN · Seen Apr 2019
Similar questions at other companies
- Find All Word Occurrences in TextSentry · PHONE SCREENSentry interview questions
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Lexicographic Search SuggestionsGoogle · ONSITE INTERVIEWGoogle interview questions
- Product Record SearchWells Fargo · OAWells Fargo interview questions
- Find Unmet Users With Shared InterestsPalantir · ONSITE INTERVIEWPalantir interview questions
- Merge Weekday and Time IntervalsNextdoor · PHONE SCREENNextdoor interview questions
- Group AnagramsWorkday · PHONE SCREEN
- Aggressive CowsAmazon · PHONE SCREEN
- Group AnagramsEPAM · PHONE SCREENEPAM interview questions
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions