# Reason About Recursive Key Search in Nested JSON
Given an in-memory JSON-like object whose values may include strings, numbers, booleans, lists, or nested objects, explain how to search the entire structure for a queried key and return its associated value. Identify the result semantics that must be clarified before writing an exact implementation.
### Constraints & Assumptions
- The input is already a structured object, not serialized JSON text.
- Lists may contain values or nested objects.
- The source shows successful lookups but does not define duplicate-key or missing-key behavior.
### Clarifying Questions to Ask
- If the key occurs at several depths or branches, should the function return one value or every match?
- Are objects and lists traversed in a defined order?
- What should a missing query return, and how should arbitrary JSON values cross the API boundary?
```hint Separate discovery from representation
First decide which matching paths count; only then choose how the corresponding structured values are returned.
```
### What a Strong Answer Covers
- Recursive or iterative traversal of objects and lists.
- A stated duplicate-key and missing-key contract rather than an invented first-match rule.
- Returning structured values without lossy or language-dependent text serialization.
- Complexity, recursion depth, cycles outside valid JSON, and representative tests.
### Follow-up Questions
1. How would you return the full path to every matching key?
2. How would you handle a structure deep enough to overflow the call stack?
Quick Answer: Explain recursive key lookup over structured nested JSON while clarifying duplicate matches, missing keys, list traversal, and portable return values.
Given an in-memory JSON-like object whose values may include strings, numbers, booleans, lists, or nested objects, explain how to search the entire structure for a queried key and return its associated value. Identify the result semantics that must be clarified before writing an exact implementation.
Constraints & Assumptions
The input is already a structured object, not serialized JSON text.
Lists may contain values or nested objects.
The source shows successful lookups but does not define duplicate-key or missing-key behavior.
Clarifying Questions to Ask Guidance
If the key occurs at several depths or branches, should the function return one value or every match?
Are objects and lists traversed in a defined order?
What should a missing query return, and how should arbitrary JSON values cross the API boundary?
What a Strong Answer Covers Guidance
Recursive or iterative traversal of objects and lists.
A stated duplicate-key and missing-key contract rather than an invented first-match rule.
Returning structured values without lossy or language-dependent text serialization.
Complexity, recursion depth, cycles outside valid JSON, and representative tests.
Follow-up Questions Guidance
How would you return the full path to every matching key?
How would you handle a structure deep enough to overflow the call stack?