Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Weighted Search Ranking
00:00
5 left

Weighted Search Ranking

MediumSQL · PostgreSQL

Problem

How would you design a search algorithm that returns weighted search results?

Assume each candidate result has a text relevance score and a popularity score. Write a SQL query that returns active results for one search phrase, calculating a weighted score using 70% text relevance and 30% popularity. Treat a missing popularity score as zero.

Output

  1. One row per included search result with result_id, title, and weighted_score
  2. Include only active results for the search phrase hvac repair
  3. Sort by weighted_score descending, then result_id ascending for ties

Schema

search_results
ColumnTypeDescription
result_idPKINTUnique identifier for a search result
search_queryVARCHAR(100)Search phrase associated with the candidate result
titleVARCHAR(255)Title displayed for the result
result_typeVARCHAR(50)Type of searchable result
relevance_scoreDECIMAL(5,2)Text matching score from 0.00 to 1.00
popularity_scoreDECIMAL(5,2)Popularity score from 0.00 to 1.00
is_activeBOOLEANWhether the result is eligible for display
Tablessearch_results
Interviewer

Your question is Weighted Search Ranking. Start with the requirements and the one table 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.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.