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
Extract Session IDs from JSON Logs
00:00
5 left

Extract Session IDs from JSON Logs

MediumPython

Problem

Chewy click logs arrive as one JSON object per line, but the file may contain blank lines, whitespace, malformed JSON records, unrelated events, and records with missing fields. Write a function that returns the unique session IDs for matching customer clicks while preserving their first appearance order.

A record matches when all of the following are true:

  1. It is valid JSON representing an object.
  2. event equals "click".
  3. customer_id equals the requested customer ID.
  4. product_id is in the requested set of product IDs.
  5. session_id is a non-empty string.

Ignore every other record without raising an exception. The input is already split into lines, so the function does not need to open a file.

Formal Specification

Implement extract_session_ids(log_lines, customer_id, product_ids), where log_lines is a list of strings, customer_id is a string, and product_ids is a list of strings. Return a list of unique session ID strings in first-seen order.

Constraints

  • 0 <= len(log_lines) <= 10^5
  • Each line contains at most 10^4 characters
  • Each parsed JSON value may contain arbitrary extra fields
  • product_ids contains at most 10^4 strings
  • Matching is case-sensitive

Function Signature

def extract_session_ids(log_lines, customer_id, product_ids):
Interviewer

Your question is Extract Session IDs from JSON Logs. 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.