Functional Programming Medical Data Queries (FDSE)
Frequency: Reported
Reported for the FDSE commercial learning track and described by the sharer as "functional programming fdse commercial learning q copied straight from hackerrank."
The candidate is given a documentation file teaching a restricted functional-programming subset of Python (filter/map/reduce/lambda, in, len, sorted, slicing) plus a patient database, then asked a chain of query tasks.
Question Tasks
- Find the names of all patients who have diabetes and are at least 18 years old.
- From the eligible patients, create a report with name, age, and risk_score = (cholesterol / 10) + (blood_sugar / 5).
- Compute total_risk — the sum of all risk scores in the report.
- Sort patients by age-adjusted risk (lowest first) = risk_score + (age * 0.5).
- Build a cumulative risk report: ranked list with name, risk_score, cumulative_risk (running total).
- Produce a summary dictionary with ranked_patients, average_risk, and total_screened_out.
Verbatim Attachment (includes the candidate's in-interview attempt)
# ============================================
# PYTHON FUNCTIONAL PROGRAMMING DOCUMENTATION
# A Functional Programming Approach to
# Medical Data Queries
# ============================================
# ============================================
# 1. DATA TYPES
# ============================================
# Number: any numeric value
42
3.5
# String: a string of characters (wrapped in quotes)
"hello"
# Boolean: a True or False value
True
False
# List: an ordered collection of items (wrapped in square brackets)
[1, 2, 3]
# Dictionary: a named collection of fields (wrapped in curly braces)
{"name": "Ada", "age": 30}
# ============================================
# 2. PATIENT RECORD SCHEMA
# ============================================
# Every patient in the database is a dictionary
# with the following structure:
#
# {
# "id": int,
# "name": str,
# "age": int,
# "conditions": list[str],
# "blood_type": str,
# "lab_results": {
# "cholesterol": float,
# "blood_sugar": float,
# "hemoglobin": float
# }
# }
# ============================================
# 3. SAMPLE DATABASE
# ============================================
patients = [
{
"id": 1,
"name": "Alice",
"age": 34,
"conditions": ["diabetes", "hypertension"],
"blood_type": "O+",
"lab_results": {"cholesterol": 220, "blood_sugar": 180, "hemoglobin": 13.5},
},
{
"id": 2,
"name": "Bob",
"age": 17,
"conditions": ["asthma"],
"blood_type": "A-",
"lab_results": {"cholesterol": 190, "blood_sugar": 95, "hemoglobin": 15.0},
},
{
"id": 3,
"name": "Clara",
"age": 45,
"conditions": ["diabetes"],
"blood_type": "O+",
"lab_results": {"cholesterol": 250, "blood_sugar": 210, "hemoglobin": 11.8},
},
{
"id": 4,
"name": "David",
"age": 29,
"conditions": [],
"blood_type": "B+",
"lab_results": {"cholesterol": 180, "blood_sugar": 88, "hemoglobin": 14.2},
},
{
"id": 5,
"name": "Elena",
"age": 62,
"conditions": ["hypertension", "arthritis"],
"blood_type": "AB+",
"lab_results": {"cholesterol": 270, "blood_sugar": 130, "hemoglobin": 12.1},
},
{
"id": 6,
"name": "Frank",
"age": 51,
"conditions": ["diabetes", "asthma"],
"blood_type": "A-",
"lab_results": {"cholesterol": 240, "blood_sugar": 195, "hemoglobin": 13.0},
},
{
"id": 7,
"name": "Grace",
"age": 22,
"conditions": ["asthma"],
"blood_type": "O-",
"lab_results": {"cholesterol": 170, "blood_sugar": 90, "hemoglobin": 14.8},
},
{
"id": 8,
"name": "Hector",
"age": 40,
"conditions": ["diabetes", "hypertension", "arthritis"],
"blood_type": "O+",
"lab_results": {"cholesterol": 260, "blood_sugar": 200, "hemoglobin": 10.5},
},
]
# ============================================
# 4. ACCESSING FIELDS
# ============================================
# Use square brackets and the key name (as a string)
# to access a field inside a dictionary:
patient = patients[0] # grab the first patient as an example
patient["name"] # "Alice"
patient["lab_results"]["cholesterol"] # 220
# ============================================
# 5. OPERATORS
# ============================================
x = 7
some_list = ["a", "b", "c"]
# Equality
x == 5 # True if x equals 5
x != 5 # True if x does NOT equal 5
# Comparison
x > 5 # greater than
x < 5 # less than
x >= 5 # greater than or equal to
x <= 5 # less than or equal to
# Logical
x > 5 and x < 10 # True if BOTH conditions are True
x == "A+" or x == "A-" # True if AT LEAST ONE is True
not "x" in some_list # flips True to False, False to True
# Lambda (anonymous function) definition
lambda x: x + 1 # defines a function that takes x and returns x + 1
lambda a, b: a + b # defines a function that takes two arguments
# ============================================
# 6. VARIABLES
# ============================================
# Variables store values for later use.
# Use = to assign a value to a variable name.
name = "Alice" # store the string "Alice" in a variable called name
# ============================================
# 7. PRINTING
# ============================================
# The print() function displays output to the console.
# This is useful for seeing intermediate results.
print(patient["name"]) # displays "Alice"
# ============================================
# 8. CORE FUNCTIONS
# ============================================
# --------------------------------------------
# filter(function, list) → filter object
# --------------------------------------------
# Keeps only items where the function returns True.
# Returns a filter object — wrap it in list() to
# get a regular list back.
#
# NOTE: The function comes FIRST, then the list.
#
# Example:
list(filter(lambda p: p["age"] >= 18, patients))
# Result: all patients who are 18 or older
# --------------------------------------------
# map(function, list) → map object
# --------------------------------------------
# Applies the function to every item in the list,
# returning a new collection of transformed items.
# Returns a map object — wrap it in list() to
# get a regular list back.
# NOTE: The function comes FIRST, then the list.
# The function can return any value — a single
# field, a computed result, or a new dictionary.
# --------------------------------------------
from functools import reduce
# reduce(function, list, start_value)
# --------------------------------------------
# Combines all items in a list into a single value
# by repeatedly applying a function.
# The function takes two arguments:
# 1. accumulator (the running result so far)
# 2. current item (the next item in the list)
# The accumulator starts at start_value, and the
# function is called once per item in the list.
#
# NOTE: You must import reduce before using it.
# (We already imported it at the top of this file.)
# NOTE: The start_value comes LAST (third argument).
# --------------------------------------------
# "value" in list
# --------------------------------------------
# Returns True if the list includes the item.
# Returns False otherwise.
# This is an OPERATOR in Python, not a function.
#
# Example:
"diabetes" in patient["conditions"]
# Result: True if the patient has diabetes
# --------------------------------------------
# len(list)
# --------------------------------------------
# Returns the number of items in a list.
#
# Example:
len(patients)
# Result: 8 (if there are 8 patients in the database)
# --------------------------------------------
# sorted(list, key=function)
# --------------------------------------------
# Returns a new list sorted from LOWEST to HIGHEST
# based on the value the key function returns.
# The key function is called on each item to
# determine its sort value.
# --------------------------------------------
# list[:n] (slice notation)
# --------------------------------------------
# Returns the first n items from a list.
# If the list has fewer than n items, returns the whole list.
#
# Example:
[10, 20, 30, 40][:2]
# Result: [10, 20]
# ============================================
# 9. COMMENTS
# ============================================
# This is a comment. Everything after # on a
# line is ignored by the computer. Use comments
# to explain your thinking!
# ============================================
# 10. QUICK REFERENCE TABLE
# ============================================
# | What You Want To Do | Python Syntax |
# |----------------------------------|-------------------------------------------------|
# | Assign a value to a variable | x = 5 |
# | Display output | print(value) |
# | Keep matching items | list(filter(func, list)) |
# | Transform every item | list(map(func, list)) |
# | Combine into one value | reduce(func, list, start) (import needed) |
# | Check if item is in a list | item in list |
# | Count items in a list | len(list) |
# | Sort by a property | sorted(list, key=func) |
# | Get first n items | list[:n] |
# | Define a small inline function | lambda p: p + 1 |
# | Access a dictionary field | record["field"] |
# | Create a dictionary | {"key": value} |
# | True / False | True / False |
# | Logical operators | and / or / not |
# (map/reduce documentation blocks repeated in original file)
# ============================================
# END OF DOCUMENTATION
# ============================================
# Find the names of all patients who have diabetes and are at least 18 years old.
#we are being asked for the names of the patients as well.
patientsWithDiabetesGreaterThan18 = list(filter(lambda patient: patient["age"] >= 18 and ("diabetes" in patient["conditions"]), patients))
patientNames = list(map(lambda patient: patient["name"], patientsWithDiabetesGreaterThan18))
#newpatients = (list(filter(lambda p: p["age"] >= 18 and ("diabetes" in p["conditions"]), patients)))
# Starting from the eligible patients (diabetic, 18+), create a report where each
# entry has:
# - name: the patient's name
# - age: the patient's age
# - risk_score: calculated as (cholesterol / 10) + (blood_sugar / 5)
patientData = list(map(lambda patient: {"name": patient["name"], "age": patient["age"], "risk_score": (patient["lab_results"]["cholesterol"]) / 10 + (patient["lab_results"]["blood_suger"] / 5)}, patientsWithDiabetesGreaterThan18))
#risk_report = (list(map(lambda p: {"name" : p["name"], "age" : p["age"], "risk_score" : p["lab_results"]["cholesterol"] / 10 + p["lab_results"]["blood_sugar"] / 5}, newpatients)))
print(patientData)
# Then, compute the total_risk — the sum of all risk scores in the report.
total_risk = (reduce(lambda acc, curr: acc + curr["risk_score"], patientData, 0))
# From the risk report, sort patients by age-adjusted risk (lowest first), calculated
# as risk_score + (age * 0.5).
sortedRisk = sorted(patientData, key = lambda p: (p["risk_score"] + (p["age"] * 0.5)))
# Then build a cumulative risk report — a ranked list where each entry includes:
# - name: the patient's name
# - risk_score: their individual risk score
# - cumulative_risk: the running total of risk scores up to and including this patient
cummulative_risk_report = reduce(lambda accumulator, patient: accumulator + [{
"name": patient["name"],
"risk_score": patient["risk_score"],
"cumulative_risk": (accumulator[-1]["risk_score"] if acc else 0) + patient["risk_score"]
}], sortedRisk, [])
# Finally, produce a summary dictionary with:
# - ranked_patients: the cumulative risk report
# - average_risk: the average risk score across all eligible patients
# - total_screened_out: how many patients from the original database of 8 were not eligible
summaryDict = {
"ranked_patients": cummulative_risk_report,
"average_risk": total_risk / len(cummulative_risk_report),
"total_screened_out": len(patientData) - len(cummulative_risk_report)
}
# #Didn't get here during interview
# risk_report = sorted(risk_report, key = lambda p: p["risk_score"] + (0.5 * p["age"]))
# cumulative_risks = reduce(lambda acc, curr: acc + [
# {
# "name" : curr["name"],
# "risk_score": curr["risk_score"],
# "cumulative_risk" : (acc[-1]["risk_score"] if acc else 0) + curr["risk_score"]
# }
# ], risk_report, [])
# final_dict = {
# "ranked_patients": cumulative_risks,
# "average_risk" : total_risk / len(cumulative_risks),
# "total_screened_out" : len(patients) - len(cumulative_risks)
# }
# print(cumulative_risks)Note: the code after "END OF DOCUMENTATION" is the candidate's in-interview attempt and contains bugs preserved from the original (e.g., blood_suger typo, acc referenced inside the accumulator lambda, cumulative_risk accumulating the previous entry's risk_score rather than its cumulative_risk, and total_screened_out computed from the wrong lists). Treat it as a candidate transcript, not a reference solution.
Source: community report, Aug 2026