KatInterview

Nested Message Schema: Size and Field Type Queries

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designparsinghash-maprecursion

Last updated

Parse a mini protobuf-style schema from text, then answer batched size and field-type queries with nested messages, forward references, and cycle and error handling.

You are given the text of a small schema written in a simplified protobuf-like language, plus a batch of queries to run against it. Write a function that parses the text, validates it, and returns one answer per query.

Schema format

The text contains zero or more message definitions:

message Name {
  type field_name = number;
  ...
}
  • A message has a name and a (possibly empty) list of fields.
  • Each field is type field_name = number;. The type is either a scalar type or the name of a message defined anywhere in the same text, including later in the text.
  • Whitespace and line layout do not matter. Tokens may be separated by any amount of whitespace, including newlines, or may sit on one line.
  • // line comments and /* ... */ block comments may appear anywhere and must be ignored.
  • Names are identifiers: a letter or underscore followed by letters, digits or underscores. Field numbers are non-negative integers. Field numbers have no effect on any answer.

Scalar sizes

Scalar typeSize in bytes
bool1
int324
float4
int648
double8

The size of a message is the sum of the sizes of its fields. A field whose type is a message contributes that message's full size. There is no per-field overhead, so a message with no fields has size 0. If a field's type is a scalar name, it is treated as that scalar.

Queries

operations[i] is applied to arguments[i]:

  • "get_size" with [message_name] gives the size of that message. If the message does not exist, the answer is None.
  • "get_type" with [message_name, field_name] gives the type of that field exactly as written in the schema, as a string (a scalar name or a message name). If the message or the field does not exist, the answer is None.

Return the answers in order as a list.

Errors

Raise ValueError in each of these cases:

  • The schema text is malformed. For example, it has an unexpected token, a missing =, ;, { or }, a missing or non-numeric field number, a missing name, or an unterminated field list.
  • Two messages share a name, or two fields in one message share a name.
  • A field's type is neither a scalar type nor a message defined in the schema. This is checked after the entire text is read, so forward references are allowed.
  • A get_size query reaches a message that, directly or through embedded messages, contains itself.
  • An operation name is not "get_size" or "get_type", or its argument list has the wrong length.
  • operations and arguments have different lengths.

Constraints

  • 0 <= len(schema_text) <= 10^5
  • 0 <= len(operations) == len(arguments) <= 10^4
  • Names are case-sensitive.
  • Embedding chains may be deep, but a valid acyclic schema will have sizes that fit in a normal Python integer.
  • A cyclic embedding only produces an error when a get_size query reaches it. get_type queries are unaffected by cycles.

Examples

Example 1

Input: schema_queries('message Point { int32 x = 1; int32 y = 2; } message Segment { Point start = 1; Point end = 2; double weight = 3; }', ['get_size', 'get_size', 'get_type', 'get_type'], [['Point'], ['Segment'], ['Segment', 'start'], ['Point', 'y']])

Output: [8, 24, 'Point', 'int32']

Example 2

Input: schema_queries('message Empty { } message Later { bool ok = 9; } message Earlier { Later child = 2; int64 value = 1; }', ['get_size', 'get_size', 'get_type'], [['Empty'], ['Earlier'], ['Earlier', 'child']])

Output: [0, 9, 'Later']

Hints

Hint 1

Split the text into tokens first (identifiers, numbers, and the symbols `{ } = ;`), dropping comments. Then parsing becomes a simple walk over a token list.

Hint 2

Store each message as a dictionary from field name to type string. Run the unknown-type check only after every message has been read, so forward references work.

Hint 3

Compute sizes recursively with memoization, and keep a set of messages currently being computed. If you reach one that is already in that set, the embedding is cyclic and you must raise an error.