Recursive Nested JSON Key Search
Reported by candidates from MongoDB's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
MongoDB reported this one in August 2026, and the title sounds scarier than it is. You get a compact JSON string and a key, and you return the first matching value as the exact substring. The hinted pattern says binary search, but the input is 20000 characters at most, so nothing gets searched that way. This is a hand-written parser plus depth-first traversal. If you've got an OA invite for MongoDB, expect to write string scanning code under pressure, and StealthCoder is there as a safety net if you blank on the parsing part during the live assessment.
The problem
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:
For an object, visit each property in textual order.
Visit a property's key before its value. If the key matches, return that property's complete value immediately.
If the key does not match, recursively search its value before moving to the next property.
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.Reported by candidates. Source: FastPrep
Pattern and pitfall
Ignore the binary-search hint. Brute force isn't the issue here, since 20000 characters allows one linear pass. The real trick is to avoid building a parsed tree and instead write a recursive descent parser that tracks indices. Write parseValue(i) that returns the end index of the value starting at i. For objects, read the key string, check it against targetKey, and if it matches, return the substring from the value start to its end. If not, recurse into the value before the next property. Arrays just loop over elements. The pitfall is order. You must check the key before descending, and a deeper match must win over a later sibling, like example 3. Another trap is returning the string with its quotes. Strings have no escapes, so scanning to the next quote is safe. Depth is capped at 200, so recursion is fine. If the parser logic slips mid-assessment, StealthCoder can cover you live.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Recursive Nested JSON Key Search cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass MongoDB's OA.
MongoDB reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Recursive Nested JSON Key Search FAQ
What's the trick in the MongoDB nested JSON key search?+
Write a recursive descent parser that works on indices and returns end positions. When a key matches, slice the original string from the value's start to its end. Don't parse into a tree and re-serialize, because you need the exact original substring.
Is binary search actually needed here?+
No. The hint is misleading. With a 20000 character limit, a single linear scan through the string is enough. The work is parsing and depth-first traversal, not searching a sorted structure. Don't waste time hunting for a sorted array.
How do I handle duplicate keys and ordering?+
Traverse strictly in textual order. Check a property's key first, return if it matches, otherwise recurse into its value before moving to the next property. That naturally makes a deeper earlier match beat a later shallow one, as in example 3.
Can I just use a built-in JSON library?+
Risky. Parsing and re-serializing may change formatting or lose duplicate keys and ordering, and you must return the exact compact substring. Tracking indices on the raw string is safer and avoids any dependence on library behavior.
How do I prepare for this in 48 hours?+
Practice writing a small recursive parser for objects, arrays, and quoted strings that returns end indices. Test the three given examples plus a NOT_FOUND case and a match on an array or object value. Keep recursion depth in mind, though 200 is safe.