KatInterview

Filter Devices by Status, Threshold, and Registration Month

Arrays & HashingAuto-graded · 18 test cases
MediumAuto-gradedArrays & Hashingjsondatetimefiltering

Last updated

Tests nested JSON navigation, timestamp parsing in UTC, date-range filtering, and numeric comparison with multiple simultaneous predicates.

You are given a collection of device records and need to count how many satisfy a set of filtering criteria.

Write a function that accepts:

  • status_filter: a string representing the required device status
  • min_threshold: a numeric value for comparison
  • month_year: a string in "MM-YYYY" format specifying a target month and year
  • devices: a list of device dictionaries

Each device dictionary may contain:

  • "id": device identifier
  • "timestamp": UTC milliseconds indicating when the device was registered
  • "status": a status string
  • "operatingParams": a nested dictionary that may contain "rootThreshold" (a number)
  • Other fields (irrelevant to this task)

Return the count of devices that meet all of the following conditions:

  1. The "status" field exactly equals status_filter (case-sensitive)
  2. The "rootThreshold" value inside "operatingParams" is strictly greater than min_threshold
  3. The device was registered in the month and year specified by month_year

If a device is missing any required field ("timestamp", "status", "operatingParams", or "rootThreshold"), exclude it from the count.

Constraints:

  • 0 ≤ len(devices) ≤ 10⁵
  • month_year is always a valid string in "MM-YYYY" format (leading zeros may or may not be present)
  • timestamp values, when present, are non-negative integers representing milliseconds since the Unix epoch
  • min_threshold and rootThreshold values are numeric (int or float)
  • All date comparisons must use UTC timezone

Examples

Example 1

Input: count_filtered_devices('active', 50, '01-2024', [{'id': 1, 'timestamp': 1704067200000, 'status': 'active', 'operatingParams': {'rootThreshold': 60}}, {'id': 2, 'timestamp': 1706745599000, 'status': 'active', 'operatingParams': {'rootThreshold': 50}}, {'id': 3, 'timestamp': 1706745600000, 'status': 'active', 'operatingParams': {'rootThreshold': 80}}, {'id': 4, 'timestamp': 1704067200000, 'status': 'inactive', 'operatingParams': {'rootThreshold': 90}}])

Output: 1

Example 2

Input: count_filtered_devices('inactive', 10, '12-2023', [{'id': 1, 'timestamp': 1703980800000, 'status': 'inactive', 'operatingParams': {'rootThreshold': 11}}, {'id': 2, 'timestamp': 1704067200000, 'status': 'inactive', 'operatingParams': {'rootThreshold': 20}}, {'id': 3, 'timestamp': 1703980800000, 'status': 'active', 'operatingParams': {'rootThreshold': 99}}])

Output: 1

Example 3

Input: count_filtered_devices('online', 0, '02-2024', [{'id': 1, 'timestamp': 1706745600000, 'status': 'online', 'operatingParams': {'rootThreshold': 1}}, {'id': 2, 'timestamp': 1709164800000, 'status': 'online', 'operatingParams': {'rootThreshold': 0}}, {'id': 3, 'timestamp': 1709251200000, 'status': 'online', 'operatingParams': {'rootThreshold': 2}}])

Output: 1

Hints

Hint 1

Convert milliseconds to seconds before creating a datetime object, and always use UTC timezone to avoid environment-dependent results.

Hint 2

Check the cheapest filters first: status comparison and threshold comparison happen before datetime conversion for better performance.

Hint 3

Use dictionary .get() to safely handle missing or optional fields without raising KeyError exceptions.