Validate a Nested Object Against a Schema
Updated
Problem statement
You are given two strings, schema and document, containing valid JSON objects from the restricted grammar below. Return true exactly when the complete document matches the schema.
- Each schema leaf is the descriptor
"string"or"number". - Each non-leaf schema value is another schema object.
- Each document leaf is a JSON string or integer, and each non-leaf value is another object.
An object matches only when it has exactly the declared fields: every schema field is present, no undeclared document field is present, and each value recursively matches its descriptor or nested schema. Field order does not matter.
The JSON-string interface is only a language-neutral execution adapter for the nested object structures used in the interview. Inputs may contain JSON whitespace. Keys and string values use letters, digits, spaces, underscores, and hyphens, so escaped characters do not occur. Both inputs are syntactically valid and every object has unique keys. In this exercise, the source's number descriptor means a signed integer.
Function
validateNestedObjectSchema(schema: String, document: String) → booleanExamples
Example 1
schema = "{\"name\":\"string\",\"location\":{\"x\":\"number\",\"y\":\"number\"}}"document = "{\"name\":\"bob\",\"location\":{\"x\":5,\"y\":6}}"return = trueBoth root fields are present, and every nested value has the declared type.
Example 2
schema = "{\"name\":\"string\",\"location\":{\"x\":\"number\"}}"document = "{\"name\":\"bob\",\"location\":{\"x\":\"5\"}}"return = falselocation.x is a string, but its schema descriptor is number.
Example 3
schema = "{\"user\":{\"id\":\"number\"}}"document = "{\"user\":{\"id\":7,\"name\":\"Lin\"}}"return = falseThe nested name field is undeclared, and matching requires the exact recursive shape.
Constraints
2 <= schema.length, document.length <= 20000.- The nesting depth of either object is at most
100. - Each object contains at most
10000total fields. - Every document integer is between
-10^9and10^9, inclusive. - Schema leaves are exactly
"string"or"number"; document leaves are strings or integers.
More Confluent Onsite Interview questions
- Implement tail -nPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Sudoku Board ValidationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Sudoku Puzzle SolverPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Sequential Phrase Search Across DocumentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Bounded Combination Sum with Signed CandidatesONSITE INTERVIEW · Seen Oct 2026
- Detect a Silent SensorONSITE INTERVIEW · Seen Sep 2026
- Build an Inverted Document IndexONSITE INTERVIEW · Seen Sep 2026
- Combination Sum with Unlimited ReuseONSITE INTERVIEW · Seen Sep 2026
- Word Search Across DocumentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Windowed Key-Value MapPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
Similar questions at other companies
- Nested JSON Schema ValidatorPinterest · ONSITE INTERVIEWPinterest interview questions
- Validate Nested JSON Against a SchemaBenchling · ONSITE INTERVIEWBenchling interview questions
- Nested Substitutions with Cycle DetectionKikoff · PHONE SCREENKikoff interview questions
- Spreadsheet Formula DependenciesAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Toy Language Generic Return-Type InferenceOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Feature Flags with Rules, Dependencies and ExposurePerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
- Template Variable ExpansionGoogle · ONSITE INTERVIEWGoogle interview questions
- JSON Path Lookup with Implicit Array WildcardsMongoDB · ONSITE INTERVIEWMongoDB interview questions
- Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEWCursor interview questions
- Domain Weight CalculationAmazon · ONSITE INTERVIEW