Search Suggestions System

Hard
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Search Suggestions System

Approach

Keep sorted suggestions at each node while inserting products, so each typed character returns precomputed top 3 without full rescanning.

Complexity Analysis

Time
O(P * L + C * A)
Space
O(P * L)

Pattern

Trie Prefix Suggestions

Why It Works

Sorting first and maintaining capped suggestion lists gives fast lookup per character and constant per-result retrieval at query time.

Updated Feb 2026