Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Compare Two JSON Structures

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

Your question is Compare Two JSON Structures. 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

OpenX services exchange nested JSON payloads for configuration and ad-serving workflows. Implement a function that returns True when two JSON-compatible Python values represent the same structure and values, and False otherwise.

Object key order must not matter. Array order must matter, and arrays must contain the same values in the same positions. JSON numbers are compared by numeric value, so 1 and 1.0 are considered equal. Do not serialize the inputs to compare them.

Formal Specification

Implement compare_json(a, b):

  • Input: a and b, each a valid JSON-compatible Python value: None, bool, int, float, str, list, or dict with string keys.
  • Output: A boolean indicating structural equality.
  • Nested objects and arrays may contain any valid JSON-compatible values.

Constraints

  • Each input contains at most 10^4 total values
  • Maximum nesting depth is 100
  • Inputs are valid JSON-compatible values
  • Dictionary keys are strings

Function Signature

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