Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
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 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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.