1527. Patients With a Condition
Model: qwen2.5-coder:14b
Topics:
FAANG asked: 0
Acceptance rate: 63.1%
Problem Breakdown
The problem requires identifying patients who have a specific medical condition from a given dataset. The input consists of two tables: Patients and Conditions. The Patients table contains patient IDs and their names, while the Conditions table contains patient IDs and their respective conditions. The task is to return the names of all patients who have at least one condition that starts with a specific prefix (e.g., “DIAB1”).
Inputs:
patients: A table with columnspatient_idandpatient_name.conditions: A table with columnspatient_idandcondition.
Outputs:
- A list of patient names who have conditions starting with the given prefix.
Implicit Constraints or Tricky Edge Cases:
- The condition string might contain multiple conditions separated by spaces.
- The prefix might be case-sensitive.
- There could be duplicate entries in either table, but we only need unique patient names in the output.
- The input tables might be empty.
Intuition
A brute-force approach would involve iterating through each patient and checking all their conditions to see if any start with the given prefix. This approach is inefficient because it requires multiple joins and string operations, leading to a high time complexity of O(n * m), where n is the number of patients and m is the average number of conditions per patient.
A better approach involves using SQL’s built-in functions to efficiently filter and join the tables. By leveraging the LIKE operator with wildcards, we can directly match conditions that start with the prefix without needing to split or iterate through each condition string.
Approach
- Join the Tables: Use an inner join between the
patientsandconditionstables on thepatient_idcolumn to combine patient information with their conditions. - Filter Conditions: Apply a filter using the
LIKEoperator to select only those rows where theconditionstarts with the given prefix. - Select Unique Patient Names: Use the
DISTINCTkeyword to ensure that each patient’s name appears only once in the result, even if they have multiple conditions matching the prefix.
Complexity Analysis
- Time Complexity: O(n + m), where n is the number of rows in the
patientstable and m is the number of rows in theconditionstable. The join operation is linear with respect to the total number of rows. - Space Complexity: O(k), where k is the number of unique patients who meet the condition criteria.
Solution
import pandas as pd
def find_patients(patients: pd.DataFrame, conditions: pd.DataFrame, prefix: str) -> pd.DataFrame:
# Join the patients and conditions tables on patient_id
merged_df = pd.merge(patients, conditions, on='patient_id')
# Filter conditions that start with the given prefix
filtered_df = merged_df[merged_df['condition'].str.startswith(prefix)]
# Select unique patient names
result_df = filtered_df[['patient_name']].drop_duplicates()
return result_df
Edge Cases Handled
- Empty Tables: If either
patientsorconditionsis empty, the function will return an empty DataFrame. - Duplicate Conditions: The use of
DISTINCTensures that each patient’s name appears only once in the output. - Case Sensitivity: The prefix matching respects case sensitivity as specified by the input.