Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Deep Compare Nested JSON

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

Your question is Deep Compare Nested 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

How would you implement a custom deep-comparison function to validate two complex nested JSON API responses?

Implement deep_compare(response_a, response_b) for already parsed JSON values. Object key order must not matter, array order must matter, and values must be compared recursively. Return True only when both responses have the same structure and values, treating JSON numeric values such as 1 and 1.0 as equal. Example: {"a": 1, "b": [2]} and {"b": [2], "a": 1} return True; [1, 2] and [2, 1] return False.

Constraints

  • Inputs are valid JSON values already parsed into Python dictionaries, lists, strings, numbers, booleans, or None.
  • Object keys are strings.
  • Object key order is ignored.
  • Array order is significant.
  • The maximum total number of nested values is 10^5.
  • The maximum nesting depth is 1,000.

Function Signature

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