Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Manipulating Complex JSON

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

Your question is Manipulating Complex JSON. 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

Synechron frontend configuration tools receive deeply nested JSON payloads that must be edited without mutating the original input. Implement ordered JSON Pointer mutations for replacing existing values and deleting object properties or array elements.

Formal Specification

Write apply_operations(document, operations), where document is a JSON-compatible Python value containing dictionaries, lists, strings, numbers, booleans, or None. Each operation is a dictionary with:

  1. op: either "set" or "delete".
  2. path: a non-empty JSON Pointer string beginning with /. Object keys are separated by /, and ~1 represents / while ~0 represents ~.
  3. value: required only for "set".

Every path is valid for the document state at the time its operation runs. A set replaces an existing object value or array element. A delete removes an object property or removes an array element, shifting later elements left. Operations must be applied in order. Return a deep-copied document and leave document unchanged.

Constraints

  • 1 <= len(operations) <= 10^4
  • The document contains at most 10^5 total values
  • Paths begin with / and are valid JSON Pointer paths
  • Every operation targets an existing value
  • Operation values contain only JSON-compatible data

Function Signature

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