Domain 1EasyCORE

Patients With a Condition

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").

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 0

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 columns patient_id and patient_name.
  • conditions: A table with columns patient_id and condition.

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

  1. Join the Tables: Use an inner join between the patients and conditions tables on the patient_id column to combine patient information with their conditions.
  2. Filter Conditions: Apply a filter using the LIKE operator to select only those rows where the condition starts with the given prefix.
  3. Select Unique Patient Names: Use the DISTINCT keyword 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 patients table and m is the number of rows in the conditions table. 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 patients or conditions is empty, the function will return an empty DataFrame.
  • Duplicate Conditions: The use of DISTINCT ensures that each patient’s name appears only once in the output.
  • Case Sensitivity: The prefix matching respects case sensitivity as specified by the input.