KatInterview

Paginated Sensor-Alert Filter with Validation

Arrays & HashingAuto-graded · 20 test cases
EasyAuto-gradedArrays & Hashingarraysfilteringpagination

Last updated

Tests ability to validate, filter, sort, and paginate structured records while handling malformed entries and boundary conditions correctly.

Background

You are building a monitoring dashboard for an industrial sensor network. Each sensor periodically reports a pressure reading. Your job is to retrieve all sensors whose readings meet an alert threshold and return one page of results.

Task

Implement get_alert_records(records, page, page_size).

Input Format

records is a list of dictionaries, each with three fields:

{
    "id":           int,         # unique sensor identifier
    "systolic_bp":  int,         # recorded pressure value
    "patient_dob":  str          # date string in "YYYY-MM-DD" format
}

Filtering Rule

Include a record if its systolic_bp is greater than or equal to 140.

Validation

Before filtering, inspect every entry:

  • If systolic_bp is None or missing, skip the entry.
  • If patient_dob is not a string in the exact format "YYYY-MM-DD" representing a real calendar date, skip the entry. Strings like "2023-2-3" or "2021-02-29" must be rejected.

Skipped entries must not appear in data and must not count toward total.

Ordering and Pagination

  1. Sort all valid, matching entries by id ascending.
  2. Treat page as 1-indexed. Return entries at positions [(page-1)*page_size, page*page_size) in the sorted list.
  3. If the requested page is beyond the last entry, return an empty data list while still reporting the correct total.

Output Format

Return a dictionary with exactly these keys:

{
    "data":  list[int],   # ids of matching entries on this page, in ascending id order
    "total": int,         # total number of valid matching entries across all pages
    "page":  int          # the page number that was requested
}

If page < 1 or page_size <= 0, return {"data": [], "total": <correct total>, "page": page}.

Constraints

  • 0 <= len(records) <= 10_000
  • Each id is a positive integer; treat duplicate ids as independent entries (stable sort preserves their relative input order).
  • systolic_bp, when present and valid, is a non-negative integer.
  • 1 <= page_size <= 1_000 for well-formed calls.
  • The function must not raise an exception for any input matching the types above.

Examples

Example 1

Input: get_alert_records([{'id': 101, 'systolic_bp': 145, 'patient_dob': '1980-05-20'}, {'id': 102, 'systolic_bp': 140, 'patient_dob': '1990-01-01'}, {'id': 103, 'systolic_bp': 130, 'patient_dob': '1975-12-12'}], 1, 2)

Output: {'data': [101, 102], 'total': 2, 'page': 1}

Example 2

Input: get_alert_records([{'id': 3, 'systolic_bp': 160, 'patient_dob': '1970-03-10'}, {'id': 1, 'systolic_bp': 141, 'patient_dob': '1985-07-22'}, {'id': 2, 'systolic_bp': 139, 'patient_dob': '1992-11-01'}], 1, 1)

Output: {'data': [1], 'total': 2, 'page': 1}

Example 3

Input: get_alert_records([{'id': 10, 'systolic_bp': 140, 'patient_dob': '2000-01-01'}, {'id': 11, 'systolic_bp': 140, 'patient_dob': '2001-02-03'}, {'id': 12, 'systolic_bp': 141, 'patient_dob': '2002-03-04'}], 2, 2)

Output: {'data': [12], 'total': 3, 'page': 2}

Hints

Hint 1

Work in a strict pipeline: validate first, then filter, then sort, then slice. Mixing these steps leads to a `total` that disagrees with the returned `data`.

Hint 2

Use `date.fromisoformat()` to parse `patient_dob`, but also verify the parsed date formats back to exactly the original string — this rejects inputs like `'2023-2-3'` that `fromisoformat` would otherwise accept.

Hint 3

Compute `total` from the full filtered list before slicing. The slice index is `(page - 1) * page_size`; Python's slice syntax handles out-of-range indices gracefully, so no extra bounds check is needed for `data` — but do handle the `page < 1 or page_size <= 0` guard separately.