Skip to main content

Patients With a Condition - Solution & Explanation

EasyDatabase6 min readAsked at: Amazon, Google, Bloomberg
Practice this problem

Problem Statement

Table: Patients

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| patient_id   | int     |
| patient_name | varchar |
| conditions   | varchar |
+--------------+---------+
patient_id is the primary key (column with unique values) for this table.
'conditions' contains 0 or more code separated by spaces. 
This table contains information of the patients in the hospital.

 

Write a solution to find the patient_id, patient_name, and conditions of the patients who have Type I Diabetes. Type I Diabetes always starts with DIAB1 prefix.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Patients table:
+------------+--------------+--------------+
| patient_id | patient_name | conditions   |
+------------+--------------+--------------+
| 1          | Daniel       | YFEV COUGH   |
| 2          | Alice        |              |
| 3          | Bob          | DIAB100 MYOP |
| 4          | George       | ACNE DIAB100 |
| 5          | Alain        | DIAB201      |
+------------+--------------+--------------+
Output: 
+------------+--------------+--------------+
| patient_id | patient_name | conditions   |
+------------+--------------+--------------+
| 3          | Bob          | DIAB100 MYOP |
| 4          | George       | ACNE DIAB100 | 
+------------+--------------+--------------+
Explanation: Bob and George both have a condition that starts with DIAB1.

Approach Overview

Problem Overview: The table contains patient records with a conditions column storing multiple condition codes separated by spaces. The task is to return patients whose condition list contains the code DIAB1 as a standalone code, not as part of another code like DIAB100.

Approach 1: String Matching with Split Function (O(n * m) time, O(m) space)

Treat the conditions column as a space-separated list of codes. Split the string into tokens and check whether one of them equals DIAB1. The algorithm iterates through each patient record, performs a split(" ") operation, then scans the resulting list for the exact code. The key insight is avoiding substring matches like DIAB100 by comparing full tokens only. This approach is straightforward and works well when processing rows in application logic using languages like Python or JavaScript. Time complexity is O(n * m) where n is the number of rows and m is the average number of condition codes per row, with O(m) temporary space for the token list. This technique relies on basic string processing.

Approach 2: Using Regular Expressions (O(n * m) time, O(1) space)

Use a regular expression that matches DIAB1 as a full word inside the string. The pattern checks either the start of the string or a preceding space, followed by DIAB1, and ensures the code ends before another character continues the token. In SQL or backend filtering logic, a regex like (^|\s)DIAB1(\s|$) guarantees correct boundaries. Each row is scanned once by the regex engine, so the complexity remains O(n * m), but it avoids allocating intermediate arrays. This approach is cleaner when regex support is available in languages like Java or C#. It directly expresses the boundary condition and is common in database-style filtering problems involving regular expressions and SQL-style pattern matching.

Recommended for interviews: The regex approach is typically preferred because it expresses the "standalone code" requirement precisely and keeps the query concise. However, explaining the split-based method first shows you understand the underlying structure of the data and the risk of partial matches. Strong candidates often mention both approaches: tokenization for clarity and regex for compact production queries.

Approach 1: Approach 1: String Matching with Split Function

This approach involves iterating over each row in the Patients table. We split the 'conditions' string by spaces and check if any of the resulting parts starts with the prefix 'DIAB1'. If so, we include that patient in our result set.

The solution defines a function get_patients_with_diab1 that takes a list of tuples as input, where each tuple represents a row from the Patients table. It iterates over each patient, splits the 'conditions' string into a list of condition codes, and checks if any of those conditions start with 'DIAB1'. If a match is found, the patient's details are added to the result list.

Code

Python

JavaScript

Complexity

Time Complexity: O(n * m), where n is the number of patients and m is the average number of conditions per patient.

Space Complexity: O(n) due to the space needed to store the result list.

Try this approach in the editor →

Approach 2: Approach 2: Using Regular Expressions

This approach utilizes regular expressions to find patients with conditions starting with 'DIAB1'. It iterates over each patient's 'conditions' and uses a regex pattern to perform the check.

This Java solution uses a regular expression to match any condition that starts with 'DIAB1'. The regex pattern is \\bDIAB1\\d*, where \\b ensures that the match occurs at a word boundary, and \\d* allows for any digits following 'DIAB1'. For each patient, the matcher checks for this pattern in the conditions string. If found, the patient's details are added to the result list.

Code

Java

C#

Complexity

Time Complexity: O(n * m), where n is the number of patients and m is the length of the conditions string.

Space Complexity: O(n), proportional to the result list size.

Try this approach in the editor →

Approach 3: Default Approach

Code

MySQL

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: String Matching with Split Function

Time Complexity: O(n * m), where n is the number of patients and m is the average number of conditions per patient.

Space Complexity: O(n) due to the space needed to store the result list.

Approach 2: Using Regular Expressions

Time Complexity: O(n * m), where n is the number of patients and m is the length of the conditions string.

Space Complexity: O(n), proportional to the result list size.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
String Matching with Split FunctionO(n * m)O(m)When processing rows in application code and you want explicit token comparison.
Regular Expression MatchingO(n * m)O(1)When regex support is available and you want concise pattern matching for word boundaries.

Video Solution

Patients With a Condition | Leetcode 1527 | Crack SQL Interviews in 50 Qs #mysql #leetcode • Learn With Chirag • 5,452 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Patients With a Condition easy or hard?
Patients With a Condition is classified as an Easy database problem. The main challenge is avoiding partial matches like DIAB100 and ensuring DIAB1 appears as a separate code in the conditions string.
Patients With a Condition Python/Java solution
Python and JavaScript solutions often split the conditions string and check whether DIAB1 appears as an exact token. Java and C# implementations frequently use regex patterns such as (^|\\s)DIAB1(\\s|$) to guarantee correct word boundaries.
How to solve Patients With a Condition in O(n)?
You iterate through the patient rows once and check the conditions string using either token comparison or a regex boundary match. Each row requires scanning the condition text, which makes the effective complexity O(n * m). Using a regex such as (^|\\s)DIAB1(\\s|$) ensures accurate detection in a single pass.
What is the best approach for Patients With a Condition?
Regular expression matching is usually the best approach because it enforces word boundaries and prevents partial matches such as DIAB100. A pattern like (^|\\s)DIAB1(\\s|$) checks that DIAB1 appears as a standalone code. The solution scans each row once with O(n * m) time complexity.
Is Patients With a Condition asked at Google/Amazon/Meta?
Database filtering problems like this appear frequently in SQL interview rounds at companies such as Amazon and Meta. They test your ability to work with string fields, pattern matching, and query conditions rather than complex algorithms.
What data structure is used in Patients With a Condition?
The problem mainly uses string processing rather than advanced data structures. The conditions column behaves like a space-separated list, so solutions rely on string tokenization or regular expression matching.
What is the time complexity of Patients With a Condition?
Both common approaches run in O(n * m) time, where n is the number of patient rows and m is the average length of the conditions string. Each row must be scanned to detect the DIAB1 code. Space complexity ranges from O(1) with regex matching to O(m) when splitting the string into tokens.

Ready to solve this problem?

Practice Patients With a Condition with our built-in code editor and test cases.

Practice on FleetCode