Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Efficient JSON Transformations

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Efficient JSON Transformations. Start with the requirements on the right.

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.

Problem

Palo Alto Networks frontend surfaces such as Cortex XSIAM may receive deeply nested JSON configuration and alert objects. Implement a function that removes every object field whose dot-separated path appears in a blocked-path list, including the field's entire subtree.

Formal Specification

Given a JSON-compatible value data and an array blocked_paths, return a new JSON-compatible value with all matching fields removed. A path such as user.credentials.token refers only to object keys, and array elements do not add path components. Apply the same path rules independently to every object inside an array. Preserve the order of object keys and array elements. Do not mutate data.

Use a trie for blocked paths so shared prefixes are represented once and a blocked subtree can be skipped without traversing its descendants. Object keys used in paths do not contain periods. The root itself is never blocked.

Constraints

  • data contains only objects, arrays, strings, numbers, booleans, and null
  • 1 <= number of blocked paths <= 10^4
  • Each path contains 1 to 100 keys
  • The total number of path components is at most 10^5
  • The JSON tree contains at most 10^5 values
  • Object keys used in paths do not contain periods
  • The root itself is never blocked

Function Signature

def filter_json(data, blocked_paths):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output