Find COVID Recovery Patients - Problem
COVID Recovery Patient Tracker

In the midst of the pandemic, hospitals need to track patient recovery patterns to understand treatment effectiveness and plan resources. You're tasked with creating a system to identify patients who have successfully recovered from COVID-19.

What defines recovery?
A patient is considered recovered if they:
โ€ข Had at least one Positive test result
โ€ข Later had at least one Negative test result on a subsequent date

Your mission:
Find all recovered patients and calculate their recovery time - the number of days between their first positive test and their first negative test after that positive test.

Database Schema:
patients table: Contains patient information (patient_id, patient_name, age)
covid_tests table: Contains test results (test_id, patient_id, test_date, result)

Return results ordered by recovery_time (ascending), then by patient_name (ascending).

Input & Output

example_1.py โ€” Basic Recovery Case
$ Input: patients: [(1, 'Alice', 30), (2, 'Bob', 25)] covid_tests: [(1, 1, '2023-01-01', 'Negative'), (2, 1, '2023-01-05', 'Positive'), (3, 1, '2023-01-15', 'Negative'), (4, 2, '2023-01-10', 'Positive')]
โ€บ Output: [(1, 'Alice', 10)]
๐Ÿ’ก Note: Alice tested positive on 2023-01-05 and negative on 2023-01-15, taking 10 days to recover. Bob only has positive tests, so he's not included.
example_2.py โ€” Multiple Patients
$ Input: patients: [(1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)] covid_tests: [(1, 1, '2023-01-05', 'Positive'), (2, 1, '2023-01-15', 'Negative'), (3, 2, '2023-01-01', 'Positive'), (4, 2, '2023-01-08', 'Negative'), (5, 3, '2023-01-03', 'Positive'), (6, 3, '2023-01-20', 'Negative')]
โ€บ Output: [(2, 'Bob', 7), (1, 'Alice', 10), (3, 'Charlie', 17)]
๐Ÿ’ก Note: Bob recovered in 7 days, Alice in 10 days, Charlie in 17 days. Results are ordered by recovery time ascending.
example_3.py โ€” Edge Case with Same Names
$ Input: patients: [(1, 'Alice', 30), (2, 'Alice', 25)] covid_tests: [(1, 1, '2023-01-05', 'Positive'), (2, 1, '2023-01-20', 'Negative'), (3, 2, '2023-01-01', 'Positive'), (4, 2, '2023-01-16', 'Negative')]
โ€บ Output: [(1, 'Alice', 15), (2, 'Alice', 15)]
๐Ÿ’ก Note: Both patients named Alice recovered in 15 days. When recovery times are equal, results maintain original order or can be ordered by patient_id.

Constraints

  • 1 โ‰ค patient_id โ‰ค 104
  • 1 โ‰ค test_id โ‰ค 105
  • patient_name consists of lowercase English letters and spaces
  • result is one of: 'Positive', 'Negative', 'Inconclusive'
  • Only patients with both positive and negative results are included
  • test_date is in YYYY-MM-DD format

Visualization

Tap to expand
Patient TimelineJan 5: PositiveJan 8: PositiveJan 15: NegativeRecovery: 10 daysFirst Positive: Jan 5First Negative After: Jan 15Patient Recovered!
Understanding the Visualization
1
Timeline Setup
Each patient has a chronological sequence of test results
2
First Positive
Identify when the patient first tested positive (illness start)
3
First Negative After
Find the first negative test that occurs after any positive test
4
Recovery Calculation
Calculate days between first positive and first subsequent negative
Key Takeaway
๐ŸŽฏ Key Insight: Use window functions to efficiently process patient timelines and conditional aggregation to find the critical recovery dates in a single database pass.
Asked in
Meta 25 Amazon 20 Google 15 Microsoft 12
21.2K Views
Medium Frequency
~18 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen