Filter Paginated Medical Records
Problem statement
Filter a paginated collection of synthetic medical records using ordinary comparisons and the average of historical weight measurements.
Finite page adapter
For this exercise, assume records concatenates all fetched pages in page order. Each full page has ten records; the final page may be shorter. Rows 0..9 form the first page, rows 10..19 form the second, and so on. An empty array represents no records. No network request is needed in this finite adapter.
Each row is [recordId, age]. All record IDs are unique. Each weightHistory row is [recordId, weight] and contributes one historical measurement, including repeated equal measurements. History rows may be interleaved and unordered; every referenced ID occurs in records.
For this exercise, assume a record qualifies exactly when all conditions hold:
- Its age is between
minAgeandmaxAge, inclusive. - It has at least one historical weight measurement.
- The arithmetic mean of all its historical weights is between
minAverageWeightandmaxAverageWeight, inclusive.
Use the exact unrounded mean. All weights and weight bounds use the same arbitrary integer unit. Return the IDs of qualifying records in their original page-and-row order. An empty result is allowed. Age fields, threshold ranges, missing-history handling and this output shape are practice definitions, not recovered original requirements.
Function
filterMedicalRecords(records: int[][], weightHistory: int[][], minAge: int, maxAge: int, minAverageWeight: int, maxAverageWeight: int) → int[]Examples
Example 1
records = [[1,25],[2,40],[3,30]]weightHistory = [[1,60],[1,80],[2,65],[3,90]]minAge = 20maxAge = 35minAverageWeight = 65maxAverageWeight = 85return = [1]Record1 has age25 and mean70. Record2 fails age; record3 fails average weight.
Example 2
records = [[1,30],[2,30],[3,30],[4,30],[5,30],[6,30],[7,30],[8,30],[9,30],[10,30],[11,30]]weightHistory = [[11,75]]minAge = 20maxAge = 40minAverageWeight = 70maxAverageWeight = 80return = [11]Only record11 has history; it is on the second page and qualifies.
Example 3
records = []weightHistory = []minAge = 0maxAge = 120minAverageWeight = 0maxAverageWeight = 10000000return = []An empty concatenation of pages has no qualifying records.
Constraints
0 <= records.length <= 256and0 <= weightHistory.length <= 512.- Every row in either matrix has exactly two integers.
- Record IDs are distinct integers from
1through10^9; every history ID belongs to one record. 0 <= age <= 120and1 <= weight <= 10^7.0 <= minAge <= maxAge <= 120.0 <= minAverageWeight <= maxAverageWeight <= 10^7.- Sums and threshold products may exceed signed32-bit range; signed64-bit arithmetic is sufficient.