Prefix Word Dictionary
Updated
Problem statement
You are building a dictionary of lowercase words that can answer prefix questions. For example, after adding "cart", "care" and "dog", the prefix "car" matches "care" and "cart".
You get two arrays of the same length, operations and arguments. Run them from first to last on a dictionary that starts empty. operations[i] is the name of operation i and arguments[i] is its string.
"add":arguments[i]is a word. Put it in the dictionary. Adding a word that is already there changes nothing."queryPrefix":arguments[i]is a prefix. Find every word in the dictionary right now that starts with this prefix.
A word starts with a prefix when its first letters are exactly the prefix. A word starts with itself, and the empty prefix "" is the start of every word.
Return a list with one answer for each "queryPrefix", in the order they run. Each answer is the list of matching words, each listed once, in alphabetical order. Compare letter by letter; a word that is a prefix of another word comes first, so "pen" is before "pencil". If no word matches, the answer is an empty list. If there is no "queryPrefix", return an empty list.
Function
runPrefixDictionary(operations: String[], arguments: String[]) → String[][]Examples
Example 1
operations = ["add","add","add","queryPrefix"]arguments = ["cart","care","dog","car"]return = [["care","cart"]]The dictionary holds "cart", "care" and "dog". Two of them start with "car". In alphabetical order, "care" comes before "cart" (the fourth letters are e and t). The answer is [["care","cart"]].
Example 2
operations = ["add","add","add","queryPrefix","queryPrefix","queryPrefix"]arguments = ["pencil","pen","pen","pen","pe","x"]return = [["pen","pencil"],["pen","pencil"],[]]Adding "pen" a second time changes nothing, so it is listed once. A word counts as starting with a prefix when it equals the prefix, so "pen" is listed for the prefix "pen", before the longer word "pencil". The prefix "pe" matches the same two words. No word starts with "x", so that answer is an empty list. The answer is [["pen","pencil"],["pen","pencil"],[]].
Example 3
operations = ["add","add","queryPrefix","add","queryPrefix"]arguments = ["tea","ten","","tee","te"]return = [["tea","ten"],["tea","tee","ten"]]The empty prefix "" matches every word, so the first query lists "tea" and "ten". After "tee" is added, the prefix "te" matches all three words in alphabetical order. The answer is [["tea","ten"],["tea","tee","ten"]].
Constraints
1 <= operations.length <= 2000arguments.length == operations.length- Each
operations[i]is"add"or"queryPrefix". - For
"add",arguments[i]is a word of 1 to 12 lowercase English letters. For"queryPrefix",arguments[i]is a prefix of 0 to 12 lowercase English letters. - The answers together contain at most 1000 words in total.
More Bloomberg LP Phone Screen questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Design File SystemGoogle · PHONE SCREENGoogle interview questions
- Long-ID Store Without HashingAdobe · PHONE SCREENAdobe interview questions
- Implement TrieOptiver · PHONE SCREENOptiver interview questions
- Word Dictionary with WildcardsDatadog · PHONE SCREENDatadog interview questions
- Design Search Autocomplete SystemRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Word Dictionary with Wildcard FindAmazon · ONSITE INTERVIEW
- Implement a TrieMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Add Word and Suffix SearchAnduril · ONSITE INTERVIEWAnduril interview questions
- In-Memory Directory and File OperationsSquarepoint Capital · ONSITE INTERVIEWSquarepoint Capital interview questions
- File System PathsCapital One · ONSITE INTERVIEWCapital One interview questions
Bloomberg LP interview experiences
- Bloomberg SWE Phone Screen and Onsite: 1 Experience
- Bloomberg SWE Phone Screens with Two Teams: 1 Experience
- Bloomberg AI Interview, Hiring Level Not Reported: 2 Experiences
- Bloomberg Experienced SWE AI Coding Interviews: 2 Experiences
- Bloomberg New Grad AI-Assisted Coding Rounds: 3 Experiences
- Bloomberg New Grad SWE AI-Assisted Coding Rounds: 1 Experience