Find the Longest Word in Dictionary through Deleting

Algorithms
Medium
Uber
62.2K views

Given a string $s$ and a dictionary of strings $d$, find the longest string in $d$ that can be formed by deleting some characters of $s$. If there are multiple long strings, return the lexicographically smallest one.

Why Interviewers Ask This

Interviewers at Uber ask this to evaluate your ability to implement efficient subsequence checking algorithms while handling complex tie-breaking logic. They specifically test if you can optimize for time complexity using a two-pointer approach rather than brute force, and whether you can correctly prioritize lexicographical order when multiple solutions exist.

How to Answer This Question

1. Clarify the problem constraints: Confirm if strings are empty and define 'deleting characters' as forming a subsequence. 2. Propose a sorting strategy: Explain that sorting the dictionary by length (descending) and then lexicographically (ascending) allows you to return the first valid match immediately. 3. Implement the helper function: Describe a two-pointer technique where one pointer tracks the source string and the other the candidate word, advancing only on matches. 4. Optimize iteration: Instead of checking all words, iterate through the sorted list and break early upon finding the first valid subsequence. 5. Analyze complexity: Explicitly state the time complexity is O(N log N + M * K), where N is dictionary size, M is average word length, and K is source length, demonstrating awareness of performance trade-offs relevant to high-scale systems like Uber's.

Key Points to Cover

  • Demonstrating knowledge of subsequence validation via two-pointer technique
  • Correctly implementing multi-key sorting for length and lexicographical order
  • Optimizing the loop to return early once the first valid match is found
  • Clearly articulating Time Complexity relative to input sizes
  • Handling edge cases like empty strings or no valid matches gracefully

Sample Answer

To solve this efficiently, I would first sort the dictionary array. The primary sort key is the string length in descending order, ensuring we check longer candidates first. The secondary sort key is lexicographical order in ascending order, which handles the tie-breaking requirement automatically. This way, the first word we find that is a valid subsequence of the source string is guaranteed to be the correct answer. Next, I would implement a helper function called isSubsequence. This uses a two-pointer approach. One pointer iterates through the source string, and another through the current dictionary word. We advance the source pointer whenever characters match or simply move forward. If the dictionary word pointer reaches the end of the word, it means all characters were found in order within the source string. Finally, we iterate through our sorted dictionary. For each word, we call isSubsequence. As soon as we return true, we stop and return that word immediately because our sorting guarantees it is the longest and lexicographically smallest among those of equal length. This approach avoids checking unnecessary words after finding the solution. In terms of complexity, sorting takes O(D log D) where D is the number of words, and checking each word takes O(S) where S is the length of the source string. This is highly efficient for large datasets, which aligns with the scalability requirements often seen in backend engineering roles at companies like Uber.

Common Mistakes to Avoid

  • Using brute force nested loops without sorting, leading to poor performance on large inputs
  • Forgetting to handle the lexicographical tie-breaker, returning an arbitrary long string instead of the smallest one
  • Implementing the subsequence check incorrectly by resetting pointers or skipping non-matching characters improperly
  • Neglecting to mention edge cases such as an empty dictionary or when no word can be formed from the source string

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 57 Uber questions