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.
declareRecordaddsrecordNamewith exactly the supplied set of field names. - Declare an alias.
declareAliasaddsaliasNamewith the supplied declared name as its target. - Resolve aliases.
areCompatiblefollows every alias link until each supplied name reaches a record shape. - Compare field sets.
areCompatiblereturnstrueexactly 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
Fieldandfieldare different.
API
| Signature | Returns | Behavior |
|---|---|---|
RecordShapeCompatibilityEngine() | Not applicable | Creates an empty declaration registry. |
declareRecord(recordName: string, fieldNames: string[]) | void | Adds one named record shape. |
declareAlias(aliasName: string, targetName: string) | void | Adds one transparent alias to an existing declaration. |
areCompatible(firstTypeName: string, secondTypeName: string) | boolean | Resolves both names and compares their record field sets. |
Examples
| Step | Operation | Result |
|---|---|---|
| 1 | declareRecord("Contact", ["email", "phone"]) | void |
| 2 | declareAlias("CustomerContact", "Contact") | void |
| 3 | declareAlias("PrimaryContact", "CustomerContact") | void |
| 4 | declareRecord("Shipping", ["phone", "email"]) | void |
| 5 | areCompatible("PrimaryContact", "Contact") | true |
| 6 | areCompatible("PrimaryContact", "Shipping") | true |
Constraints
- A type or field name contains 1 to 32 ASCII letters or digits and starts with an ASCII letter.
fieldNamescontains 1 to 32 unique names.- Every
recordNameandaliasNameis globally unique within one engine. - Every
targetNamesupplied todeclareAliasalready names a record or alias in the same engine. - Every name supplied to
areCompatiblealready 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.