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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Precision@K for Retrieval Evaluation
00:00
5 left

Precision@K for Retrieval Evaluation

MediumPython

Problem

Pearson Revel ranks learning resources for each learner query. Given the ranked document IDs returned by the search system and the known relevant document IDs, compute Precision@K for every query and the mean Precision@K across all queries.

Precision@K is the number of relevant documents among the first K retrieved results divided by K. If fewer than K documents are returned, missing ranks count as non-relevant, so the denominator remains K.

Formal Specification

Implement evaluate_precision_at_k(retrieved_results, relevant_results, k). retrieved_results and relevant_results are dictionaries mapping query IDs to lists of unique document ID strings. Every query in retrieved_results appears in relevant_results. k is a positive integer. Return a dictionary containing one floating-point Precision@K value for each query, plus a mean_precision value containing the arithmetic mean of the query scores.

Constraints

  • 1 <= number of queries <= 10^4
  • 1 <= k <= 10^5
  • Each query may return at most 10^5 document IDs
  • Document IDs are non-empty strings
  • Document IDs within each ranked result list are unique
  • Every query in retrieved_results appears in relevant_results

Function Signature

def evaluate_precision_at_k(retrieved_results, relevant_results, k):
Interviewer

Your question is Precision@K for Retrieval Evaluation. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.