Katkat

Parse a protobuf-like schema and answer queries

Applied IntuitionSoftware Engineer · Technical Screen

Last updated

This problem tests your ability to parse a hierarchical schema definition, resolve nested type references using a symbol table, and implement an efficient lookup function for type paths.

Problem Description

You are tasked with building a schema resolver for a custom configuration language. The schema is defined as a list of strings where each string is either a base type declaration or a nested type definition.

Input format:

  • message Name { ... }: Defines a message type containing fields.
  • field_type field_name: Defines a field within a message.
  • Nested messages are allowed.

Your task is to implement a class SchemaParser that processes a list of schema lines and a method resolve_type(query) that returns the full path (e.g., "MsgA.SubMsg.Field") of a given field or returns "NOT_FOUND".

Signature

class SchemaParser:
    def __init__(self, schema_lines: List[str]):
        # Initialize and build symbol table
        pass

    def resolve_type(self, query: str) -> str:
        # Resolve query string to full path or return "NOT_FOUND"
        pass

Rules

  1. A field inside MessageA can be accessed as MessageA.field_name.
  2. A field inside a nested message MessageB within MessageA is accessed as MessageA.MessageB.field_name.
  3. Assume no two messages share the same name within the same scope.

Example

Input lines:

message Outer {
  message Inner {
    int32 value
  }
  string name
}

Queries:

  • resolve_type("Outer.name") -> "Outer.name"
  • resolve_type("Outer.Inner.value") -> "Outer.Inner.value"

Constraints

  • The input is a list of lines representing a valid schema structure.
  • query will be a dot-separated string.
  • Depth of nesting is at most 10.

Hints

Hint 1

Use a stack to keep track of the current nesting level while iterating through the schema lines.

Hint 2

Store the schema in a nested dictionary (a Trie-like structure) where each node represents a message or a field.

Hint 3

For the resolution function, split the query string by '.' and traverse the nested dictionary to check if the path exists.

Follow-up questions

  • How would you handle type aliasing (e.g., 'type Alias = ExistingType')?
  • What changes are needed if the schema allows forward references to types not yet defined in the file?
  • How would you optimize the lookups if you need to resolve millions of queries against the same schema?