Detect Cross-Doctor Prescription Pairs Within 30 Days
Problem statement
Each date-sorted record is [patientId, doctorId, drugName, YYYY-MM-DD].
Return every distinct patientId + " " + drugName pair for which two different doctors prescribed that drug to that patient within at most 30 days of each other. Emit a pair once, when it first qualifies in input order. Dates are valid ISO calendar dates.
Function
findCrossDoctorPrescriptions(records: String[][]) → String[]Examples
Example 1
records = [["P1","D1","Atorvastatin","2025-01-10"],["P1","D2","Atorvastatin","2025-02-05"],["P2","D3","Metformin","2025-03-01"],["P2","D4","Metformin","2025-03-25"]]return = ["P1 Atorvastatin","P2 Metformin"]Both patient-drug pairs have different doctors within 30 days.
Example 2
records = [["P1","D1","A","2025-01-01"],["P1","D2","A","2025-02-01"]]return = []The records are 31 days apart, so the pair does not qualify.
Example 3
records = []return = []No records produce no pairs.
Constraints
0 <= records.length <= 100000.- Records are sorted by nondecreasing date.
- Identifiers and drug names are nonempty and contain no spaces.