Longest Word in Dictionary

Longest Word in Dictionary is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Design Patterns pattern, in the Tries subpattern.

Problem statement

Given an array of strings words representing an English Dictionary, return the longest word in words that can be built one character at a time by other words in words.

If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string.

Example 1: Input: words = ["w","wo","wor","worl","world"] Output: "world" Explanation: The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".

Example 2: Input: words = ["a","banana","app","appl","ap","apply","apple"] Output: "apple" Explanation: Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".

Constraints: 1 <= words.length <= 1000 1 <= words[i].length <= 30 words[i] consists of lowercase English letters.

Topics and companies

Topics: Hash Table, Trie.

Reported in interviews at Goldman Sachs, Facebook.

How to practise Longest Word in Dictionary on Thita.ai

Starter code is provided in C, C#, C++, dart, elixir, erlang, Go, Java, JavaScript, Kotlin, PHP, Python, python3, racket, Ruby, Rust, Scala, Swift and TypeScript. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(t) space.

Open Longest Word in Dictionary in the code editor, or read the Longest Word in Dictionary editorial for a worked solution with its approach and complexity analysis.

Where Longest Word in Dictionary sits in the DSA pattern sheet

Problems related to Longest Word in Dictionary

Other problems that use the same Tries and Design Patterns techniques:

Opening the problem

Progress is saved as you study.