As reported: MongoDB Software Engineer 3, first-stage coding interview (2025), two parts. Part 1: design an object representation for a subset of JSON that contains only objects, arrays and strings. Part 2: implement
pathQuery(json, path), wherepathis a list of property names, returning every object, array or string the path refers to — the path passes through arrays.
Part 1 — representation. Design classes to hold a JSON document built only from objects, arrays and strings, nested to any depth. The top level may be an object or an array. For example:
{"a": "foo", "b": "bar"}
{"b": ["foo", "bar"]}
{"a": {"b": [{"c": "foo", "d": {}, "e": "bar", "f": {"g": "baz"}}]}}
{"a": [{"b": "foo"}, {"c": "bar"}, {"b": "baz"}]}
["val1", "val2", {"a": "foo"}]
Part 2 — path query. Implement pathQuery(jsonText, path): parse jsonText into your representation, then follow path, a list of property names, and return every value it reaches, in document order:
- On an object, the next name selects that property (no match if it is absent).
- On an array, the path continues into every element with the same remaining names — arrays are transparent.
- On a string, a remaining name matches nothing.
- When all names are used up, the value reached is a result — whatever it is, an array included.
Return each result serialised as compact JSON: no whitespace, object keys in their original order.
Examples
{"a":"foo"} ["a"] -> "foo"
{"a":{"b":"foo"}} ["a"] -> {"b":"foo"}
{"a":{"b":[{"c":"foo","d":{},"e":"bar"}]}} ["a","b","c"] -> "foo"
{"a":{"b":[{"c":"foo"},{"c":"baz"}]}} ["a","b","c"] -> "foo", "baz"
["val1","val2",{"a":"foo"}] ["a"] -> "foo"
{"b":["foo","bar"]} ["b"] -> ["foo","bar"]
Constraints: jsonText is valid JSON of this subset, up to 3·10^4 characters, nesting depth ≤ 100, and may contain spaces and newlines between tokens; strings contain only letters, digits, spaces, _, - and . (no escapes); keys start with a letter and are unique within an object; 1 ≤ path.length ≤ 50.
Code runner format: two JSON values, one per line: jsonText as a JSON string, then path. Implement pathQuery(jsonText, path) returning the list of serialised results; the harness prints them joined as one JSON array — for the fourth example, ["foo","baz"]. Built-in JSON parsers are fine in Python and JavaScript, but the point of part 1 is your own representation.