Problemtoy language type compatibility engine

Session: Sign in to solve

Solution.txt

Record Shape Compatibility Engine

Problem

Design RecordShapeCompatibilityEngine, an in-memory registry of named record shapes and transparent aliases that compares the shapes identified by two declared names.

Requirements

  • Declare a record. declareRecord adds recordName with exactly the supplied set of field names.
  • Declare an alias. declareAlias adds aliasName with the supplied declared name as its target.
  • Resolve aliases. areCompatible follows every alias link until each supplied name reaches a record shape.
  • Compare field sets. areCompatible returns true exactly when the two resolved records contain the same field names; declaration order does not matter.
  • Compare field names exactly. Field-name equality is case-sensitive, so names such as Field and field are different.

API

SignatureReturnsBehavior
RecordShapeCompatibilityEngine()Not applicableCreates an empty declaration registry.
declareRecord(recordName: string, fieldNames: string[])voidAdds one named record shape.
declareAlias(aliasName: string, targetName: string)voidAdds one transparent alias to an existing declaration.
areCompatible(firstTypeName: string, secondTypeName: string)booleanResolves both names and compares their record field sets.

Examples

StepOperationResult
1declareRecord("Contact", ["email", "phone"])void
2declareAlias("CustomerContact", "Contact")void
3declareAlias("PrimaryContact", "CustomerContact")void
4declareRecord("Shipping", ["phone", "email"])void
5areCompatible("PrimaryContact", "Contact")true
6areCompatible("PrimaryContact", "Shipping")true

Constraints

  • A type or field name contains 1 to 32 ASCII letters or digits and starts with an ASCII letter.
  • fieldNames contains 1 to 32 unique names.
  • Every recordName and aliasName is globally unique within one engine.
  • Every targetName supplied to declareAlias already names a record or alias in the same engine.
  • Every name supplied to areCompatible already names a record or alias in the same engine.
  • At most 200 declarations and 1,000 public method calls occur per testcase.
  • Inputs satisfy these constraints.

Notes

Calls are sequential.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.