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
Extract Filtered Events from JSON
00:00
5 left

Extract Filtered Events from JSON

EasyPython

Problem

Acme Analytics stores application logs as a large JSON payload. Write a function that parses the payload and extracts only the events that match a target event type and optional attribute filters.

Given a JSON string representing an object with an events array, return a list of simplified event objects for every valid event whose type matches target_type and whose key-value pairs in required_attrs are all present inside the event's attributes object.

Formal Specification

Implement:

  • payload: a JSON string
  • target_type: a string
  • required_attrs: a dictionary of required attribute filters

Return a list of dictionaries. Each output dictionary must contain:

  • id: event id
  • timestamp: event timestamp
  • user_id: user id if present, otherwise null

Ignore malformed event entries, events missing required top-level fields (id, type, timestamp), and events whose attributes field is not an object.

Constraints

  • 1 <= len(events) <= 10^5
  • The JSON payload may be invalid; return an empty list in that case
  • Each valid event is a JSON object
  • Required top-level fields are id, type, and timestamp
  • required_attrs may be empty, in which case all events of the target type qualify

Function Signature

def extract_events(payload, target_type, required_attrs):
Interviewer

Your question is Extract Filtered Events from JSON. 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.