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 for Multi-Class
00:00
5 left

Precision and Recall for Multi-Class

EasyPython

Problem

The RealReal evaluates a classifier that assigns categories to fashion listings. Given the true category and predicted category for each listing, calculate precision and recall independently for every requested class.

Formal Specification

Implement multiclass_precision_recall(y_true, y_pred, labels), where y_true, y_pred, and labels are lists of strings. y_true[i] is the correct class for item i, and y_pred[i] is the model's prediction. The lists y_true and y_pred have equal length. Return a dictionary mapping each label to a dictionary containing numeric precision and recall values.

For class c:

  • precision = TP / (TP + FP), where the denominator is the number of predictions equal to c.
  • recall = TP / (TP + FN), where the denominator is the number of true labels equal to c.
  • If either denominator is zero, return 0.0 for that metric.

Preserve the labels supplied by the caller, including classes that do not appear in either input. Do not calculate macro or micro averages.

Constraints

  • 1 <= len(y_true) = len(y_pred) <= 10^5
  • 1 <= len(labels) <= 10^4
  • Labels are unique, non-empty strings
  • Every value in y_true and y_pred appears in labels

Function Signature

def multiclass_precision_recall(y_true, y_pred, labels):
Interviewer

Your question is Precision and Recall for Multi-Class. 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.