FastPrepRecursive Nested JSON Key Search
Problems/Recursive Nested JSON Key Search

Recursive Nested JSON Key Search

MongoDB logoMongoDB● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a MongoDB phone screen for a full-time role. The report leaves out the traversal order, how duplicate keys are handled, what happens when a key is missing, and how to write the output. We filled those in.

Our version is about 82% the same as the reported question.

Given a compact JSON string json and a property name targetKey, recursively search every object and array for the first property whose key exactly equals targetKey.

The JSON value tree contains only objects, arrays, and strings. Object properties remain in their textual order, arrays remain in index order, and duplicate property names are allowed.

Use depth-first, left-to-right traversal:

  1. For an object, visit each property in textual order.
  2. Visit a property's key before its value. If the key matches, return that property's complete value immediately.
  3. If the key does not match, recursively search its value before moving to the next property.
  4. For an array, recursively search elements from index 0 upward.

Return the matched value as the exact compact JSON substring from json. A string value therefore includes its surrounding quotation marks. If no key matches, return NOT_FOUND.

Function

findNestedJsonValue(json: String, targetKey: String) → String

Examples

Example 1

json = "{\"departments\":[{\"name\":\"Strategy\",\"lead_strategist\":\"Sarah Chen\"},{\"name\":\"Data\",\"lead_strategist\":\"Omar Ali\"}]}"targetKey = "lead_strategist"return = "\"Sarah Chen\""

The first matching property appears in the first department object, so the returned JSON value is the string literal "Sarah Chen".

Example 2

json = "{\"teams\":{\"primary\":{\"team\":\"search\",\"members\":[\"Ada\",\"Lin\"]},\"backup\":{\"team\":\"ads\"}}}"targetKey = "primary"return = "{\"team\":\"search\",\"members\":[\"Ada\",\"Lin\"]}"

The matching property stores an object, so the complete compact object value is returned with its original property order.

Example 3

json = "{\"a\":{\"target\":\"deep\"},\"target\":\"shallow\"}"targetKey = "target"return = "\"deep\""

Depth-first traversal searches the value of property a before visiting the later root property, so the nested value wins.

Constraints

  • 1 <= json.length <= 20000
  • 1 <= targetKey.length <= 100
  • json is valid compact JSON with no whitespace outside strings.
  • Every value is an object, array, or string, and the maximum nesting depth is 200.
  • Strings contain only English letters, digits, spaces, underscores, and hyphens, so no escape sequences occur.
  • Property order and duplicate property occurrences are significant.

More MongoDB interview questions

  • String Interpolation with a Missing-Value ModePHONE SCREEN · Seen Nov 2026
  • Build Packages as Dependencies Are DiscoveredPHONE SCREEN · Seen Oct 2026
  • Lazy K-Way Union IteratorPHONE SCREEN · Seen Oct 2026
  • Finite Web CrawlerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Predicate Expression Tree EvaluatorPHONE SCREEN · Seen Aug 2026
  • JSON Path Lookup with Implicit Array WildcardsONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
  • Parse Nested Message TypesApplied Intuition · PHONE SCREENApplied Intuition interview questions
  • Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
  • Positive-Integer Expression EvaluationJane Street · PHONE SCREENJane Street interview questions
  • Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Flatten JSON With Array IndicesAdobe · PHONE SCREENAdobe interview questions
  • Decode StringSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
  • Plus and Minus CalculatorIndeed · PHONE SCREENIndeed interview questions
  • Arithmetic Expression EvaluatorMicrosoft · PHONE SCREENMicrosoft interview questions
  • Toy Language Generic Return-Type InferenceOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions

MongoDB interview experiences

  • MongoDB Experienced SWE Interviews (Atlas Growth): 1 Experience
All MongoDB interview questionsSee MongoDB hiring insights
Saved
class Solution {
    public String findNestedJsonValue(String json, String targetKey) {
        // Write your solution here
    }
}

Not a valid String

Checking account…