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
Merge and Deduplicate Retrieval Results
00:00
5 left

Merge and Deduplicate Retrieval Results

MediumPython

Problem

T. Rowe Price's research search experience receives ranked document results from multiple retrieval sources. Merge these results, keep only the best occurrence of each document, and return the highest-ranked documents in a deterministic order.

Implement merge_results(results, source_priority, top_k).

Each record contains doc_id, score, title, and source. A document's best occurrence is the record with the highest score. If scores tie, prefer the source with the lower priority value. If both score and priority tie, prefer the occurrence encountered first. Return the selected records sorted by descending score, ascending source priority, and ascending encounter order. Return at most top_k records.

Formal Specification

  • results is a list of result lists. Each inner list is ordered, but its order does not determine the final ranking.
  • source_priority is a dictionary mapping every source name to an integer priority.
  • top_k is a nonnegative integer.
  • Return a list of the original record dictionaries, with no duplicate doc_id values.

Constraints

  • 1 <= len(results) <= 100
  • 0 <= sum(len(batch) for batch in results) <= 10^5
  • 0 <= top_k <= 10^5
  • Document IDs are non-empty strings
  • Every record source exists in source_priority
  • Scores are numeric

Function Signature

def merge_results(results, source_priority, top_k):
Interviewer

Your question is Merge and Deduplicate Retrieval Results. 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.