Filter Formatted Records by Criteria
Updated
Problem statement
You are given a list of records, each stored as one string, and a list of criteria. Return the records that meet every criterion.
A record is one or more fields joined by commas, and each field is written name:value, for example "city:seattle,price:450000,beds:3". A record never repeats a field name, and different records may have different fields.
A criterion is a field name, an operator and a value written together with no spaces, for example "price<=500000" or "city!=tacoma". The operator is one of =, !=, <, <=, > and >=. A record meets a criterion when it has a field with that name and comparing the field's value with the criterion's value using the operator gives true. Integer values are compared as numbers. A record that has no field with that name does not meet the criterion, whatever the operator.
Return the records that meet every criterion, exactly as written and in their original order. If no record does, return an empty array.
Function
filterRecords(records: String[], criteria: String[]) → String[]Examples
Example 1
records = ["city:seattle,price:450000,beds:3","city:tacoma,price:380000,beds:4","city:seattle,price:720000,beds:4","city:seattle,price:500000,beds:2"]criteria = ["city=seattle","price<=500000"]return = ["city:seattle,price:450000,beds:3","city:seattle,price:500000,beds:2"]The first and the last record have city equal to seattle and a price of at most 500000. The last one passes with a price of exactly 500000, because <= also accepts an equal value. The second record is in tacoma, and the third costs 720000.
Example 2
records = ["id:7,status:sold","id:12","id:31,status:active","id:40,status:pending"]criteria = ["status!=sold"]return = ["id:31,status:active","id:40,status:pending"]"id:31,status:active" and "id:40,status:pending" have a status other than sold. "id:12" has no status field, so it does not meet the criterion, even though the operator is !=.
Example 3
records = ["beds:10,baths:2","beds:9,baths:3","beds:12,baths:1"]criteria = ["beds>9","baths>=2"]return = ["beds:10,baths:2"]As numbers, 10 and 12 are greater than 9, and 9 is not. Of those two records, only "beds:10,baths:2" has at least 2 baths. Comparing the values as text would go wrong here: the text "10" sorts before "9".
Example 4
records = ["city:reno,price:200000","city:boise,price:310000"]criteria = ["price>=100000","pool=yes"]return = []Both records meet price>=100000, but neither has a pool field, so neither meets pool=yes. The answer is empty.
Constraints
1 <= records.length <= 10003 <= records[i].length <= 1001 <= criteria.length <= 103 <= criteria[j].length <= 32- Each
records[i]is one or morename:valuefields joined by commas, with no spaces, and no field name appears twice in the same record. - Each
criteria[j]is a field name, then one of the operators=,!=,<,<=,>,>=, then a value, with no spaces. - A field name is 1 to 10 lowercase English letters. A value is either a word of 1 to 20 lowercase English letters or an integer from
0to10^9written without leading zeros. - A field name holds the same kind of value (always an integer or always a word) in every record that has it, and a criterion on that name uses a value of the same kind. Criteria with
<,<=,>or>=always use an integer.
More Zillow Phone Screen questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Jump GamePHONE SCREEN · Seen Dec 2022
- Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- Two Sum: All PairsPHONE SCREEN · Seen Nov 2022
- Employee Free TimePHONE SCREEN · Seen Oct 2022
- First Unique Character in a StringPHONE SCREEN · Seen Oct 2022
- K-th Smallest Number in a StreamPHONE SCREEN · Seen Oct 2022
Similar questions at other companies
- Detect a Straight FlushTemu · PHONE SCREEN
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREEN
- Single-Condition Product FilterCrowdstrike · PHONE SCREEN
- Convert an Entry List to a MapEPAM · PHONE SCREEN
- Most Frequent NameAmazon · PHONE SCREEN
- Words with Repeated AnagramsThe Voleon Group · PHONE SCREEN
- Project JSON Fields into a Target FormatBooking.com · PHONE SCREEN
- Product Keyword SearchTesco · PHONE SCREEN
- Verify Words Sorted by a Custom AlphabetTrueFoundry · PHONE SCREEN
- Character Frequencies Across Nested String ListsSnowflake · PHONE SCREEN