107. DoorDash Restaurant Search Engine
DoorDash Restaurant Search Engine
You are building a restaurant search engine for DoorDash using a trie (prefix tree).
You are given an initial list of restaurant names such as
["panda express", "panera bread"]. Each restaurant name must be stored so that you can:
- insert a new restaurant name,
- check whether an exact restaurant name exists,
- check whether any stored restaurant name starts with a given prefix,
- return up to
k restaurant names that start with a given prefix.
The search engine stores only unique restaurant names. If the same restaurant name appears multiple times in the constructor input or is inserted multiple times later, it is still stored only once.
If fewer than
k restaurant names match the prefix, return all matching names. If no restaurant name matches the prefix, return an empty list.
Returned restaurant names must be sorted in lexicographically ascending order.
Class to Implement
Implement the
RestaurantSearchEngine class.
Method Signatures
Constructor
RestaurantSearchEngine(List<String> restaurantNames)
- Initializes the search engine with the given list of restaurant names.
- Each restaurant name in
restaurantNames should be inserted.
- Duplicate names in
restaurantNames should be stored only once.
- If the input list is empty, the search engine starts with no restaurant names.
Insert Restaurant
void insertRestaurant(String restaurantName)
- Inserts
restaurantName into the search engine.
- If
restaurantName is already stored, this call does nothing.
Check Exact Restaurant
boolean containsRestaurant(String restaurantName)
- Returns
true if restaurantName is currently stored.
- Returns
false otherwise.
Check Prefix
boolean startsWith(String prefix)
- Returns
true if there is at least one stored restaurant name that starts with prefix.
- Returns
false otherwise.
Get Matching Restaurants
List<String> getTopKRestaurants(String prefix, int k)
- Returns up to the lexicographically smallest
k stored restaurant names that start with prefix.
- If fewer than
k restaurant names match, return all matching restaurant names.
- If no restaurant name matches, return an empty list.
Input Rules
- Each restaurant name contains only lowercase English letters and single spaces.
- A restaurant name does not start or end with a space.
- A restaurant name does not contain consecutive spaces.
- Each prefix contains only lowercase English letters and spaces.
- A prefix does not start or end with a space.
- A prefix does not contain consecutive spaces.
- An empty prefix is not allowed.
- The trie must treat the space character as a valid stored character.
Constraints
0 ≤ restaurantNames.size() ≤ 3 * 104
1 ≤ restaurantName.length() ≤ 2000
1 ≤ prefix.length() ≤ 2000
1 ≤ k ≤ 3 * 104
- At most
3 * 104 total calls will be made to insertRestaurant, containsRestaurant, startsWith, and getTopKRestaurants.
Examples
Example 1
RestaurantSearchEngine searchEngine = new RestaurantSearchEngine( restaurantNames = List.of("panda express", "panera bread") ); searchEngine.containsRestaurant(restaurantName = "panda express"); Output:
true searchEngine.containsRestaurant(restaurantName = "pan"); Output:
false searchEngine.startsWith(prefix = "pan"); Output:
true searchEngine.getTopKRestaurants(prefix = "pan", k = 2); Output:
["panda express", "panera bread"] Explanation:
"panda express" exists exactly in the search engine.
"pan" is only a prefix, not a complete restaurant name.
- Both stored restaurant names start with
"pan".
Example 2
RestaurantSearchEngine searchEngine = new RestaurantSearchEngine( restaurantNames = List.of("panda express", "panera bread", "burger king") ); searchEngine.insertRestaurant(restaurantName = "pancake house"); Output:
no return value searchEngine.getTopKRestaurants(prefix = "pan", k = 3); Output:
["pancake house", "panda express", "panera bread"] searchEngine.startsWith(prefix = "bur"); Output:
true searchEngine.getTopKRestaurants(prefix = "bur", k = 5); Output:
["burger king"] Explanation:
- After insertion, three stored restaurant names start with
"pan".
- They are returned in lexicographically ascending order.
- Only one restaurant name starts with
"bur".
- Since fewer than
k names match "bur", return all matches.
Example 3
RestaurantSearchEngine searchEngine = new RestaurantSearchEngine( restaurantNames = List.of("taco bell", "thai garden") ); searchEngine.startsWith(prefix = "pa"); Output:
false searchEngine.getTopKRestaurants(prefix = "pa", k = 2); Output:
[] Explanation:
- No stored restaurant name starts with
"pa".
- Therefore, the prefix check returns
false.
- The search result is an empty list.
Example 4
RestaurantSearchEngine searchEngine = new RestaurantSearchEngine( restaurantNames = List.of("panda express", "panda express", "panera bread") ); searchEngine.getTopKRestaurants(prefix = "pan", k = 5); Output:
["panda express", "panera bread"] searchEngine.insertRestaurant(restaurantName = "panda express"); Output:
no return value searchEngine.getTopKRestaurants(prefix = "pan", k = 5); Output:
["panda express", "panera bread"] Explanation:
- Duplicate names in the constructor input are stored only once.
- Reinserting an existing restaurant name does not create duplicates.