Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Validating Complex JSON Responses
00:00
5 left

Validating Complex JSON Responses

HardPython

Problem

Pureintegration QA tests need to verify that an integration response has the expected nested JSON structure. Implement a recursive validator that checks objects, arrays, primitive types, required fields, and minimum array lengths against a supplied schema.

Formal Specification

Write validate_response(response, schema):

  • response is a JSON-compatible Python value containing dictionaries, lists, strings, numbers, booleans, or None.
  • schema is a dictionary node with a type field.
  • An object schema uses properties, a dictionary mapping field names to child schemas, and an optional required list.
  • An array schema uses an items child schema and may define minItems.
  • Primitive schemas use types string, integer, number, boolean, or null.
  • Return True only when the complete response satisfies the schema. Unknown object fields are allowed. Return False for missing required fields, incorrect types, invalid nested values, or invalid array lengths.

A JSON boolean must not be accepted as an integer, even though Python makes bool a subclass of int.

Constraints

  • The response and schema contain at most 10^4 total nodes.
  • Nesting depth is at most 100.
  • Object field names are strings.
  • Schemas use object, array, string, integer, number, boolean, and null node types.
  • Unknown response object fields are allowed.

Function Signature

def validate_response(response, schema):
Interviewer

Your question is Validating Complex JSON Responses. 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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.