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
Filter and Map for UI Data
00:00
5 left

Filter and Map for UI Data

MediumPython

Problem

The Times of India homepage receives article objects from multiple feed sources. Prepare a UI-ready list of article cards by filtering eligible articles, removing duplicate IDs, ranking the remaining articles, and selecting a maximum number of results.

Implement prepare_feed(items, category, min_score, limit).

Requirements

  1. Keep only articles where isPublished is True, category matches the requested category, and score is at least min_score.
  2. If an article ID appears more than once, keep the occurrence with the highest score. If scores are equal, keep the earliest occurrence.
  3. Sort retained articles by descending score. Preserve original input order when scores are equal.
  4. Return at most limit objects, mapped to only these fields: id, title, imageUrl, and score.
  5. Do not mutate the input array or its objects. Return an empty array when no article qualifies.

Formal Specification

Input is an array of objects with fields id and title as strings, category as a string, isPublished as a boolean, score as an integer, and imageUrl as a string. category is a string, min_score is an integer, and limit is a non-negative integer. Return an array of mapped objects in ranked order.

Examples

Example 1: items = [{id: "a", title: "A", category: "sports", isPublished: true, score: 80, imageUrl: "a.jpg"}, {id: "b", title: "B", category: "sports", isPublished: true, score: 95, imageUrl: "b.jpg"}], category = "sports", min_score = 70, limit = 2 returns [{id: "b", title: "B", imageUrl: "b.jpg", score: 95}, {id: "a", title: "A", imageUrl: "a.jpg", score: 80}].

Example 2: Duplicate IDs retain the higher-scoring article, while unpublished or mismatched articles are excluded.

Constraints

  • 1 <= items.length <= 10^5
  • 0 <= limit <= items.length
  • Scores are between 0 and 10^9.

Constraints

  • 1 <= items.length <= 10^5
  • 0 <= limit <= items.length
  • 0 <= min_score <= 10^9
  • 0 <= items[i].score <= 10^9
  • Each item contains all fields specified in the formal input definition

Function Signature

def prepare_feed(items, category, min_score, limit):
Interviewer

Your question is Filter and Map for UI 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.