Return Top Department Suggestions
GleanSoftware Engineer · Technical ScreenLast updated
Design an in-memory prefix search system that returns the top-k department suggestions based on popularity scores, with deterministic tie-breaking.
Problem Statement
In an enterprise search platform, users frequently search for internal departments (e.g., "Engineering - Infrastructure", "Enterprise Sales"). To improve discoverability, implement an autocomplete suggestion system that returns the top k department names matching a given prefix.
Implement the DepartmentSearch class:
DepartmentSearch(departments: list[tuple[str, int]]): Initializes the search system with a list of pairs(name, score), wherenameis a unique department name andscoreis its search popularity score.suggest(prefix: str, k: int) -> list[str]: Returns up tokdepartment names that start withprefix, ordered by:- Highest score first.
- Lexicographically smaller name in case of a score tie.
If fewer than k departments match the prefix, return all matching departments in the specified order. If no departments match, return an empty list.
Example
search = DepartmentSearch([
("engineering", 100),
("enterprise sales", 80),
("engineering operations", 100),
("energy", 50),
("executive", 90)
])
# "en" matches "engineering", "enterprise sales", "engineering operations", "energy"
search.suggest("en", 3)
# Output: ["engineering", "engineering operations", "enterprise sales"]
# Explanation: "engineering" and "engineering operations" both have score 100, but "engineering" is lexicographically smaller.
search.suggest("exec", 2)
# Output: ["executive"]
search.suggest("finance", 3)
# Output: []
Constraints
1 <= len(departments) <= 20,0001 <= len(name) <= 100for each department name0 <= score <= 10^61 <= len(prefix) <= 1001 <= k <= 100- Department names and prefixes consist of lowercase English letters and spaces.
suggestwill be called up to10,000times.
Edge Cases
kis larger than the number of matching departments.prefixmatches zero departments.prefixmatches the exact full name of a department.
Hints
Hint 1
A Trie (prefix tree) allows you to traverse down the characters of the query prefix in time proportional to the prefix length.
Hint 2
To avoid searching all descendants on every query, consider precomputing and storing the top-k suggestions at each Trie node during construction.
Hint 3
When sorting or pushing to a heap for tie-breaking, use a compound key like `(-score, name)` so that identical scores are ordered alphabetically.
Follow-up questions
- How would you support real-time updates where department scores change frequently without rebuilding the entire Trie?
- If the Trie size exceeds the memory of a single machine, how would you partition the index across multiple nodes?
- How would you modify the data structure to support fuzzy matching (e.g., tolerance of 1 typo or edit distance)?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.