Resolve Field Paths in a Nested Message Schema
Stacks & QueuesAuto-graded · 13 test casesLast updated
Parse brace-delimited nested message definitions line by line, then check which dotted query paths name an actual field, tracking scope with a stack.
Nested Schema Field Lookup
You are given a small schema written in a configuration language, as a list of text lines. You are also given a list of dotted path queries. For each query, decide whether it names a field declared in the schema. If it does, return the query unchanged. Otherwise return "NOT_FOUND".
Schema format
Each line of schema_lines is one of the following, ignoring leading and trailing whitespace:
- Blank line. Ignore it.
- Message opening:
message Name {. This starts a message calledName. It is nested inside the message that is currently open, if there is one. - Message closing: a line that is just
}. This closes the most recently opened message that is still open. - Field declaration: exactly two whitespace-separated words,
field_type field_name. This declares a field calledfield_nameinside the message that is currently open.
Messages can be nested inside other messages.
Paths
The full path of a field is the names of all enclosing messages from outermost to innermost, followed by the field name, all joined with ..
For example, a field value inside message Inner, which is inside message Outer, has the path Outer.Inner.value.
No two messages in the same scope share a name.
Task
Implement resolve_schema_queries(schema_lines, queries). It returns a list with one entry per query, in the same order:
- If the query is exactly the full path of a declared field, the entry is the query itself.
- Otherwise, the entry is
"NOT_FOUND". This includes queries that name a message rather than a field, queries that use the wrong scope, and queries naming things that do not exist.
Matching is exact and case-sensitive.
Constraints
schema_linesdescribes a valid schema with balanced braces.- Each query is a dot-separated string.
- Nesting depth is at most 10.
- A field declaration line always has exactly two words, and it always appears inside an open message.
Examples
Example 1
Input: resolve_schema_queries(['message Outer {', ' message Inner {', ' int32 value', ' }', ' string name', '}'], ['Outer.name', 'Outer.Inner.value', 'Outer.Inner', 'Outer.missing'])
Output: ['Outer.name', 'Outer.Inner.value', 'NOT_FOUND', 'NOT_FOUND']
Hints
Hint 1
You do not need a tree. Build one set containing the full dotted path of every field, then answer each query with a membership check.
Hint 2
Keep a stack of the names of the messages that are currently open. Push on a `message Name {` line, pop on `}`, and build full paths by joining the stack with the name.
Hint 3
A line with exactly two words is a field. A query that names a message, not a field, must not match, so only field paths go in the set.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Do one pass over the schema lines and keep a stack of the names of the messages that are currently open.
- On a
message Name {line, pushNameonto the stack. - On a
}line, pop the stack. - On a two-word line, treat it as a field. Join the stack and the field name with
.to get its full path, and add that path to a set. - Skip blank lines.
After the pass, each query is answered by checking whether it is in the set of field paths. If it is, return it. If not, return "NOT_FOUND".
Why it works
At every point in the scan, the stack holds exactly the messages whose opening line has been seen but whose closing } has not. Its top is the innermost enclosing message. So joining the stack with a field name gives that field's true full path, which matches the scoping rules.
Only fields are inserted into the set. Message paths such as Outer.Inner are never added, so a query naming a message correctly yields "NOT_FOUND". A field that is looked up under the wrong scope produces a path that was never added, so it also yields "NOT_FOUND". A field declared after a nested message closes is attached to the outer message, because the pop already happened.
Complexity
- Time: O(S + Q * L), where S is the total size of the schema text, Q is the number of queries, and L is the length of a query. Building each path costs time proportional to its length, and each set lookup hashes the query string.
- Space: O(F * L) for the set of field paths, plus O(D) for the stack, where D is at most 10.
Reference solution
from typing import List
def resolve_schema_queries(schema_lines: List[str], queries: List[str]) -> List[str]:
field_paths = set()
open_messages = []
for raw_line in schema_lines:
line = raw_line.strip()
if not line:
continue
if line == "}":
if open_messages:
open_messages.pop()
continue
if line.startswith("message ") and line.endswith("{"):
name = line[len("message "):-1].strip()
open_messages.append(name)
continue
parts = line.split()
if len(parts) == 2 and open_messages:
field_paths.add(".".join(open_messages + [parts[1]]))
return [q if q in field_paths else "NOT_FOUND" for q in queries]