Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Precision and Recall on Imbalanced Data
00:00
5 left

Precision and Recall on Imbalanced Data

EasyPython

Problem

Vertafore may evaluate classification models used with products such as AMS360, where positive cases can be much less common than negative cases. Given actual and predicted binary labels, calculate precision and recall without using machine learning libraries.

Formal Specification

Implement precision_recall(y_true, y_pred), where both inputs are non-empty lists of integers containing only 0 and 1, and have equal length. Label 1 is the positive class. Return a dictionary with two floating-point values:

  • precision = TP / (TP + FP), or 0.0 when the model predicts no positive cases.
  • recall = TP / (TP + FN), or 0.0 when the input contains no actual positive cases.

Count each label pair once. Do not round the results.

Constraints

  • 1 <= len(y_true) = len(y_pred) <= 10^6
  • Every value in both lists is either 0 or 1
  • The positive class is represented by 1
  • The input lists have equal length
  • Return 0.0 for either metric when its denominator is zero

Function Signature

def precision_recall(y_true, y_pred):
Interviewer

Your question is Precision and Recall on Imbalanced Data. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.