FastPrepValidate Nested JSON Against a Schema
Problems/Validate Nested JSON Against a Schema

Validate Nested JSON Against a Schema

Benchling logoBenchling● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Benchling full-time onsite interview. We follow the exercise in the report. The report leaves out some input limits and some interface details. We chose those. Our version is about 95% the same as the reported question.

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 inputs are JSON strings only so that every language can read them. The interview itself used nested objects. 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) → boolean

Examples

Example 1

schema = "{\"name\":\"string\",\"location\":{\"x\":\"number\",\"y\":\"number\"}}"document = "{\"name\":\"bob\",\"location\":{\"x\":5,\"y\":6}}"return = true

The fields name and location are both present. name is a string, and x and y are numbers, so the document matches the schema.

Example 2

schema = "{\"name\":\"string\",\"location\":{\"x\":\"number\"}}"document = "{\"name\":\"bob\",\"location\":{\"x\":\"5\"}}"return = false

location.x is written as text in quotes, so it is a string, but the schema asks for a number. The document does not match.

Example 3

schema = "{\"user\":{\"id\":\"number\"}}"document = "{\"user\":{\"id\":7,\"name\":\"Lin\"}}"return = false

The document has an extra field name inside user that the schema does not declare. A match needs exactly the declared fields, so the answer is false.

Constraints

  • 2 <= schema.length, document.length <= 20000.
  • The nesting depth of either object is at most 100.
  • Each object contains at most 10000 total fields.
  • Every document integer is between -10^9 and 10^9, inclusive.
  • Schema leaves are exactly "string" or "number"; document leaves are strings or integers.

More Benchling interview questions

  • Degenerate DNA Substring SearchONSITE INTERVIEW · Seen Jun 2024
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Jul 2021
  • Bounded HTTP Retry FlowPHONE SCREEN · Seen Nov 2025
  • Future-Aware Cache EvictionPHONE SCREEN · Seen Aug 2025
  • Generate All Contiguous Protein ChainsPHONE SCREEN · Seen Jul 2024
  • Eight-Direction Keyboard Typing TimePHONE SCREEN · Seen Apr 2022
  • Generate Proteins from Contiguous Named SequencesPHONE SCREEN · Seen Aug 2021
  • Next Palindrome NumberPHONE SCREEN · Seen Jul 2021
  • Apply Overlapping HTML Formatting RangesPHONE SCREEN · Seen Feb 2019
  • 2048 Move SimulatorOA · Seen Feb 2019

Similar questions at other companies

  • Nested JSON Schema ValidatorPinterest · ONSITE INTERVIEWPinterest 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
All Benchling interview questionsSee Benchling hiring insights
Saved
class Solution {
    public boolean validateNestedObjectSchema(String schema, String document) {
        // Write your code here.
    }
}
Checking account…