Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
Start free
Search, Filter, and Group Metadata
00:00
5 left

Search, Filter, and Group Metadata

MediumPython

Problem

Scribd's discovery systems receive an unsorted array of document metadata. Implement a function that searches documents by text, applies dynamic filters, and groups the matching document IDs by a requested metadata field.

The search is case-insensitive and matches the document's title, author, or any value in tags. A blank or None query matches every document. Filters are supplied as a dictionary:

  1. A scalar value requires exact equality.
  2. A list requires a scalar document field to appear in that list, or requires at least one overlap when the document field is a list such as tags.
  3. A range object such as {"min": 2015, "max": 2024} applies inclusive bounds. Either bound may be omitted.

Return a dictionary mapping each group value to a list of matching document IDs. Preserve the input order within each group and omit groups with no matches.

Formal Specification

Implement search_filter_group(documents, query, filters, group_by), where documents is a list of dictionaries, query is a string or None, filters is a dictionary, and group_by is a metadata field name. Return a dictionary whose keys are string representations of group values and whose values are lists of IDs.

Constraints

  • 1 <= len(documents) <= 10^5
  • Each document contains id, title, author, tags, category, language, and year
  • tags is a list of strings, and year is an integer
  • Filter fields and group_by are valid document fields

Function Signature

def search_filter_group(documents, query, filters, group_by):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.