cswork / libraryOpen practice workspace

Coding questions · Kneron

Count IoT devices matching date and threshold

This question evaluates handling of paginated REST APIs, JSON parsing and nested-data filtering, month/year date parsing, and numeric threshold comparisons, reflecting skills in data retrieval and programmatic data manipulation.

Implement function numDevices(statusQuery, threshold, dateStr) that returns the number of IoT devices from a paginated REST API that were added in the month and year given by dateStr (format MM-YYYY) and have operatingParams.rootThreshold > threshold. Query all pages of: https://jsonmock.hackerrank.com/api/iot_devices/search?status=<statusQuery>&page=<pageNumber> (replace placeholders). The API returns JSON with fields: page, per_page, total, total_pages, data (array of devices). Each device has: id, timestamp (UTC ms when added), status, operatingParams { rotorSpeed, slack, rootThreshold }, asset { id, alias }, optional parent { id, alias }. Count and return the number of matching devices.

Count IoT devices by month/year and threshold

You are given a table of IoT devices. Each row represents a device with the timestamp when it was added, its status, and operating parameters.

Write an SQL query that returns the number of devices that:

  • have a given status (for example, 'RUNNING'),
  • have operating parameter root_threshold greater than a given numeric threshold,
  • and were added in the month and year specified by a given string in the format 'MM-YYYY'.

For the sample data below and for the specific values:

  • status = 'RUNNING'
  • threshold = 40
  • month/year string = '05-2025'

your query should return the count of such devices as a single row with a single column named device_count.

Table iot_devices

columntypeconstraints
idINTPRIMARY KEY, NOT NULL
added_atTIMESTAMPNOT NULL
statusVARCHAR(20)NOT NULL
rotor_speedINTNOT NULL
slackDECIMAL(6,3)NOT NULL
root_thresholdINTNOT NULL
asset_idINTNOT NULL
asset_aliasVARCHAR(100)NOT NULL
parent_idINT
parent_aliasVARCHAR(100)

Sample data:

added_atasset_aliasasset_ididparent_aliasparent_idroot_thresholdrotor_speedslackstatus
2025-05-05 10:00:00Pump-A1011Line-110015012000.5RUNNING
2025-05-20 15:30:00Pump-B1022Line-110014513000.45RUNNING
2025-05-10 09:15:00Valve-A1033Line-210023511000.6RUNNING
2025-05-08 12:00:00Motor-A1044Line-110016010000.55STOPPED
2025-04-10 08:00:00Pump-C1055Line-310035512500.52RUNNING
2024-05-10 08:00:00Compressor-A1066Line-410047014000.48RUNNING
2025-05-30 18:45:00Valve-B1077Line-21002809000.51IDLE
2025-06-01 01:00:00Pump-D1088Line-310039013500.495RUNNING

Expected output

A single-row, single-column result with the count of devices that have status = 'RUNNING', root_threshold > 40, and were added in May 2025 (month/year '05-2025'). For the sample data, devices with id 1 and 2 match, so the count is 2.

device_count
2

Hints

  1. Parse the 'MM-YYYY' string to a date using a function like TO_DATE and treat it as the first day of that month.
  2. Filter added_at between the start of the month (inclusive) and the start of the next month (exclusive) to capture all rows in that month.