Skip to content
Decoder

Search Suggestions System

Medium

Problem

You are building the suggestion box of a shop's search bar. You are given a list of products and the searchWord a customer types, one letter at a time.

After each letter typed, suggest at most three products that start with what has been typed so far. If more than three match, pick the three that come first in alphabetical order. Return one list of suggestions per typed letter.

Example 1

products = ["mobile", "mouse", "moneypot", "monitor", "mousepad"], searchWord = "mouse"

Example 2

→ [["mobile", "moneypot", "monitor"],

Example 3

   ["mobile", "moneypot", "monitor"],

Example 4

   ["mouse", "mousepad"],

Example 5

   ["mouse", "mousepad"],

Example 6

   ["mouse", "mousepad"]]

Example 7

Input
products = ["havana"], searchWord = "tatiana"
Output
[[], [], [], [], [], [], []]

Constraints

  • 1 <= products.length <= 1000
  • 1 <= products[i].length <= 3000
  • 1 <= sum(products[i].length) <= 2 * 10^4
  • All products are unique and lowercase.
  • 1 <= searchWord.length <= 1000

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.