Design a Prefix-Based Word Search System

Difficulty: EASYCategory: LLDFrequency: 1Asked On 2025-01-15

Problem Statement

Design and implement a data structure that allows you to efficiently:

•Store a large dictionary of words
•Find and return all words that start with a given prefix

The system should be optimized for fast prefix lookups, even with millions of words.

🎯 Functional Requirements

Implement the following methods:

1. void insert(String word)

•Adds the given word to the dictionary.
•All words consist of lowercase English letters a-z.

2. List<String> getWordsWithPrefix(String prefix)

•Returns a list of all words in the dictionary that start with the given prefix.
•Words should be returned in lexicographical order (optional unless specified).
•If no words are found, return an empty list.


Solution

Comments