AlgoMaster Logo
AlgoMasterValidate Protobuf Schema Evolutionhard

Validate Protobuf Schema Evolution

hard

Protocol Buffers preserve compatibility through stable field numbers and wire types. Removing a field without reserving its number can let a future field accidentally reinterpret old data, while changing a field's wire type can make existing serialized messages unsafe to read.

Design a ProtobufCompatibilityChecker class:

  • ProtobufCompatibilityChecker() creates a stateless checker.
  • boolean isBackwardCompatible(int[][] oldFields, int[][] newFields, int[] reservedNumbers) validates a schema change.

Each field is [fieldNumber, wireType]. Return true only when all these rules hold:

  1. Field numbers are unique within oldFields and within newFields.
  2. Reserved numbers are unique.
  3. A field number present in both schemas keeps the same wire type.
  4. Every old field number absent from the new schema appears in reservedNumbers.
  5. No new field uses a reserved number.

Row order does not matter. Supported wire types are 0, 1, 2, and 5; inputs contain only supported values.

Example 1:

Input:

Output:

Explanation: Existing field numbers keep their wire types, and adding field 3 is safe.

Example 2:

Input:

Output:

Explanation: Field 2 was removed, but its number is reserved so it cannot be reused accidentally.

Constraints

  • 0 <= oldFields.length, newFields.length, reservedNumbers.length <= 1000
  • oldFields[i].length == newFields[i].length == 2
  • 1 <= fieldNumber <= 2^29 - 1
  • wireType is one of 0, 1, 2, or 5.
  • Inputs may contain duplicate numbers; duplicates make the result false.
  • At most 100 calls are made to isBackwardCompatible.
Hints

Loading...
CallReturns
new ProtobufCompatibilityChecker()null
isBackwardCompatible([[1,2],[2,0]], [[1,2],[2,0],[3,2]], [])true

Fields 1 and 2 keep their wire types, and field 3 is an additive change using a new number.

Run checks these cases. Submit also runs a larger hidden set.