FastPrepProduct Keyword Search
Problems/Product Keyword Search

Product Keyword Search

Tesco logoTesco● EasyFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Tesco report asks you to take a list of products, each with an id and a name, and return the products whose name contains the text a customer searched for. In the report's examples, a search for Milk returns products 1 to 4, and a search for Chocolate Milk returns products 2, 3 and 4.

The report does not spell out the matching rules, and its examples do not fit a plain substring search. Here, letter case is ignored, every search word must equal a whole word of the name in any order, extra spaces are ignored, and results keep the input order. You return only the ids of the matching products.

Our version is about 90% the same as the reported question.

An online store has a list of products. Product i has the id productIds[i] and the name productNames[i]. A customer searches the store by typing a query made of one or more keywords.

A word is a run of characters with no spaces in it. In a product name and in the query, words are separated by one or more spaces, and there may also be spaces at the start or the end.

A product matches the query when every keyword in the query is equal to some word in the product name, ignoring letter case. The keywords can appear in the name in any order, and they do not need to be next to each other. A keyword must equal a whole word: milk matches Milk, but it does not match Milky. If a keyword appears more than once in the query, the name still needs it only once.

Return the ids of all matching products, in the same order as the products appear in the input. If no product matches, return an empty array.

Function

searchProducts(productIds: int[], productNames: String[], query: String) → int[]

Examples

Example 1

productIds = [1,2,3,4,5]productNames = ["Amul milk","Nandini Chocolate Milk","Milk Chocolate","Chocolate Milk","Milky Way Chocolate Bar"]query = "Milk"return = [1,2,3,4]

Products 1 to 4 each have the word Milk. Product 1 writes it as milk, and letter case is ignored. Product 5 has the word Milky, which is a different word, so it is left out.

Example 2

productIds = [1,2,3,4,5]productNames = ["Amul milk","Nandini Chocolate Milk","Milk Chocolate","Chocolate Milk","Milky Way Chocolate Bar"]query = "Chocolate Milk"return = [2,3,4]

The query has two keywords, Chocolate and Milk. Products 2, 3 and 4 have both words, in either order. Product 1 has no Chocolate, and product 5 has Chocolate but no Milk.

Example 3

productIds = [10,20,30,40]productNames = ["Whole Wheat Bread","bread rolls","Breadsticks","Garlic BREAD Loaf"]query = "  bread  "return = [10,20,40]

The spaces around bread only separate words, so the query has one keyword, bread. Products 10, 20 and 40 have that word in some letter case. Breadsticks is a longer word, so product 30 does not match.

Example 4

productIds = [7,3]productNames = ["Green Tea","Lemon Cake"]query = "Lemon Tea"return = []

Product 7 has Tea but no Lemon. Product 3 has Lemon but no Tea. Neither product has both keywords, so the answer is an empty array.

Constraints

  • 1 <= productIds.length <= 10^4
  • productNames.length == productIds.length
  • 1 <= productIds[i] <= 10^9
  • All values in productIds are distinct.
  • 1 <= productNames[i].length <= 100
  • 1 <= query.length <= 100
  • productNames[i] and query contain only English letters, digits, and spaces, and each has at least one letter or digit.

Similar questions at other companies

  • Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
  • Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
  • Single-Condition Product FilterCrowdstrike · PHONE SCREENCrowdstrike interview questions
  • Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
  • Most Frequent NameAmazon · PHONE SCREEN
  • Words with Repeated AnagramsThe Voleon Group · PHONE SCREEN
  • Project JSON Fields into a Target FormatBooking.com · PHONE SCREEN
  • Verify Words Sorted by a Custom AlphabetTrueFoundry · PHONE SCREEN
  • Character Frequencies Across Nested String ListsSnowflake · PHONE SCREENSnowflake interview questions
  • Neighboring Record IDsZipRecruiter · PHONE SCREENZipRecruiter interview questions
See Tesco hiring insights
Saved
class Solution {
    public int[] searchProducts(int[] productIds, String[] productNames, String query) {
        // Write your code here.
    }
}
Checking account…