Skip to content
EasyArraysAI interview only

Count Items Matching A Rule

Asked atamazongoogleapple

01 · Problem

You are given an array items where items[i] = [type_i, color_i, name_i] describes one item, plus a rule given by two strings ruleKey and ruleValue.

ruleKey names which field of each item to look at ("type", "color" or "name"); an item matches when that field equals ruleValue exactly.

Comparisons are exact and case-sensitive. Return the number of items that match the rule (0 if none do).

02 · Examples

Example 01
Input
items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"
Output
1

Only the second item has color "silver".

Example 02
Input
items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"
Output
2

The first and third items have type "phone". The second item is named "phone" but its type is "computer", so it does not match.

Example 03
Input
items = [["tv","black","sony"],["laptop","black","dell"]], ruleKey = "name", ruleValue = "apple"
Output
0

No item is named "apple".

03 · Constraints

  • 011 <= items.length <= 104
  • 021 <= type_i.length, color_i.length, name_i.length, ruleValue.length <= 10
  • 03ruleKey is one of "type", "color" or "name"
  • 04All strings consist only of lowercase English letters

04 · Optimal complexity

Time
O(n * L)
Space
O(1)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.