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
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

Your question is Search, Filter, and Group Metadata. 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.