Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.