Nested JSON Schema Validator
Updated
Problem statement
You are given two strings, schema and document. Each string contains valid JSON from the restricted grammar below. Return true exactly when the complete document matches the schema.
A schema node is one of:
"string","integer", or"boolean"for a primitive JSON value;{"array": childSchema}for an array whose every element matcheschildSchema;{"object": {"field": fieldSchema, ...}}for an object.
An object matches only when it has exactly the declared fields: every declared field is present and no undeclared field is present. Field order does not matter. An empty array matches any array schema. The document grammar contains strings, integers, booleans, arrays, and objects; it does not contain null or decimal numbers.
The two inputs may contain JSON whitespace. Strings and field names use only letters, digits, spaces, underscores, and hyphens, so escaped characters do not occur. Both inputs are guaranteed to be syntactically valid, every object has unique keys, and schema is a valid schema.
Function
validateNestedJsonSchema(schema: String, document: String) → booleanExamples
Example 1
schema = "{\"object\":{\"profile\":{\"object\":{\"name\":\"string\",\"active\":\"boolean\"}},\"scores\":{\"array\":\"integer\"}}}"document = "{\"profile\":{\"name\":\"Ada\",\"active\":true},\"scores\":[3,5]}"return = trueEvery required field is present with the exact recursive type, and every score is an integer.
Example 2
schema = "{\"object\":{\"profile\":{\"object\":{\"name\":\"string\",\"active\":\"boolean\"}}}}"document = "{\"profile\":{\"name\":\"Ada\",\"active\":\"true\"}}"return = falseThe value of active is a string, not a JSON boolean.
Example 3
schema = "{\"object\":{\"user\":{\"object\":{\"id\":\"integer\"}}}}"document = "{\"user\":{\"id\":7,\"name\":\"Lin\"}}"return = falseObject validation is exact, so the undeclared nested field name makes the document invalid.
Constraints
1 <= schema.length, document.length <= 20000- The nesting depth of either input is at most
100. - The document contains at most
10000JSON nodes. - Every integer is between
-10^9and10^9, inclusive. - Inputs satisfy the restricted, valid JSON guarantees in the statement.
More Pinterest Onsite Interview questions
- Minimum Bus Routes to a DestinationPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delete and Compact a Forest SubtreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Filter Pins by Excluded IDsONSITE INTERVIEW · Seen Oct 2026
- Top-Scoring Items in a Time WindowONSITE INTERVIEW · Seen Oct 2026
- Optimal Account BalancingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Escape Room LeaderboardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Policy Violation Set ChecksONSITE INTERVIEW · Seen Sep 2026
- Shortest Sequence of Source SubsequencesONSITE INTERVIEW · Seen Sep 2026
- Insert Addition and Multiplication OperatorsONSITE INTERVIEW · Seen Sep 2026
- Unique Active Pins by Time SegmentONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Validate Nested JSON Against a SchemaBenchling · ONSITE INTERVIEWBenchling interview questions
- Validate a Nested Object Against a SchemaConfluent · ONSITE INTERVIEWConfluent 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