Three Smallest Product Suggestions for Every Typed Prefix

Quick Overview

Three Smallest Product Suggestions for Every Typed Prefix. After each next character of searchWord is typed, suggest up to three product names having the entire typed prefix.

Three Smallest Product Suggestions for Every Typed Prefix

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem After each next character of searchWord is typed, suggest up to three product names having the entire typed prefix. If more than three match, choose the three lexicographically smallest. Return one ordered suggestion list for every typed prefix. ```cpp class Solution { public: vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord); }; ``` ### Examples **Example 1:** ```text Input: products = ["cat","car","cart","dog"], searchWord = "ca" Output: [["car","cart","cat"],["car","cart","cat"]] Explanation: The same three c-prefixed products also share ca, in that lexicographic order. ``` **Example 2:** ```text Input: products = ["ant","bat"], searchWord = "az" Output: [["ant"],[]] Explanation: a matches ant, but az matches nothing. ``` ### Constraints - There are 1 to 1000 distinct product strings. - Each product has length 1 to 3000; their total length is 1 to 20000. - searchWord has length 1 to 1000. - All strings use lowercase English letters. ### Contract considerations The prefix must include every character typed so far. This is the original single callable, not a new stateful search class. ```hint Read the boundary condition How many output lists are required when some typed prefixes have fewer than three matching products? ``` ### Boundary reminders The prefix must include every character typed so far. This is the original single callable, not a new stateful search class.

Overview: Three Smallest Product Suggestions for Every Typed Prefix. After each next character of searchWord is typed, suggest up to three product names having the entire typed prefix.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

After each next character of searchWord is typed, suggest up to three product names having the entire typed prefix. If more than three match, choose the three lexicographically smallest. Return one ordered suggestion list for every typed prefix.

class Solution {
public:
    vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord);
};

Examples

Example 1:

Input: products = ["cat","car","cart","dog"], searchWord = "ca"
Output: [["car","cart","cat"],["car","cart","cat"]]
Explanation: The same three c-prefixed products also share ca, in that lexicographic order.

Example 2:

Input: products = ["ant","bat"], searchWord = "az"
Output: [["ant"],[]]
Explanation: a matches ant, but az matches nothing.

Constraints

  • There are 1 to 1000 distinct product strings.
  • Each product has length 1 to 3000; their total length is 1 to 20000.
  • searchWord has length 1 to 1000.
  • All strings use lowercase English letters.

Contract considerations

The prefix must include every character typed so far. This is the original single callable, not a new stateful search class.

Boundary reminders

The prefix must include every character typed so far. This is the original single callable, not a new stateful search class.