FastPrepSingle-Condition Product Filter

Single-Condition Product Filter

Crowdstrike logoCrowdstrike● EasyFULLTIMEPHONE SCREEN

Problem statement

A catalog has n products. Product i has the id ids[i], the category tags categoryTags[i], and the price prices[i].

One filter rule decides which products match. The rule has up to two criteria and an operator:

  • Category criterion (ruleCategoryTags): a product passes when at least one of its tags equals one of the rule's tags. Tags are compared without regard to letter case, so "ELECTRONICS" matches "electronics". An empty ruleCategoryTags means the rule has no category criterion.
  • Price criterion (minPrice and maxPrice): a product passes when its price is at least minPrice and at most maxPrice. Both bounds are inclusive. A negative bound is not set and is skipped. If both bounds are negative, the rule has no price criterion.

A criterion the rule does not set is not applicable and is ignored. ruleOperator is "AND" or "OR":

  • With "AND", a product matches when it passes every applicable criterion.
  • With "OR", a product matches when it passes at least one applicable criterion.
  • If the rule has no applicable criterion, every product matches, whatever the operator.

Return the ids of the matching products in the same order as the input. Return an empty array when no product matches.

Function

filterProducts(ids: String[], categoryTags: String[][], prices: double[], ruleCategoryTags: String[], minPrice: double, maxPrice: double, ruleOperator: String) → String[]

Examples

Example 1

ids = ["p1","p2","p3","p4","p5"]categoryTags = [["ELECTRONICS","HOME"],["KITCHEN"],["Books"],["home","garden"],[]]prices = [199.99,35.5,12.0,89.0,5.0]ruleCategoryTags = ["electronics","kitchen"]minPrice = 30.0maxPrice = 100.0ruleOperator = "AND"return = ["p2"]
  • Category criterion: p1 has ELECTRONICS and p2 has KITCHEN, which match electronics and kitchen when case is ignored. p3, p4, and p5 have no matching tag.
  • Price criterion: only p2 (35.5) and p4 (89.0) are between 30.0 and 100.0.
  • With "AND", only p2 passes both.

Example 2

ids = ["p1","p2","p3","p4","p5"]categoryTags = [["ELECTRONICS","HOME"],["KITCHEN"],["Books"],["home","garden"],[]]prices = [199.99,35.5,12.0,89.0,5.0]ruleCategoryTags = ["electronics","kitchen"]minPrice = 30.0maxPrice = 100.0ruleOperator = "OR"return = ["p1","p2","p4"]

The same rule with "OR". p1 passes the category criterion, p2 passes both, and p4 passes the price criterion. p3 and p5 pass neither. The ids keep the input order.

Example 3

ids = ["p1","p2","p3","p4","p5"]categoryTags = [["ELECTRONICS","HOME"],["KITCHEN"],["Books"],["home","garden"],[]]prices = [199.99,35.5,12.0,89.0,5.0]ruleCategoryTags = []minPrice = -1.0maxPrice = 35.5ruleOperator = "AND"return = ["p2","p3","p5"]

The rule has no category criterion and no minimum price, so only price <= 35.5 is checked. The bound is inclusive, so p2 (35.5) matches, along with p3 (12.0) and p5 (5.0). The unset category criterion does not make products fail under "AND".

Constraints

  • 1 <= n <= 500
  • ids.length == categoryTags.length == prices.length == n
  • 1 <= ids[i].length <= 10, and ids[i] contains only lowercase English letters and digits.
  • 0 <= categoryTags[i].length <= 4
  • 0 <= ruleCategoryTags.length <= 5
  • Every tag in categoryTags and ruleCategoryTags has 1 to 20 characters, and every character is an English letter (A to Z or a to z).
  • 0 <= prices[i] <= 10^6
  • -1 <= minPrice, maxPrice <= 10^6. A negative value means that bound is not set.
  • ruleOperator is "AND" or "OR".

More Crowdstrike problems

See Crowdstrike hiring insights
Saved
public String[] filterProducts(String[] ids, String[][] categoryTags, double[] prices, String[] ruleCategoryTags, double minPrice, double maxPrice, String ruleOperator) {
    // Write your code here.
}
Checking account…