KatInterview

Resolve Field Paths in a Nested Message Schema

Stacks & QueuesAuto-graded · 13 test cases
MediumAuto-gradedStacks & Queuesparsingstackhash-set

Last 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 called Name. 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 called field_name inside 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_lines describes 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.