KatInterview

Assign Persistent Table Column Identifiers

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashingarraysstringsdesign-ds

Last updated

Process sequential database schema snapshots to assign permanent, case-insensitive unique column aliases with automatic collision resolution.

You are building a schema tracking system that generates consistent, case-insensitive column identifiers across updates for multiple database tables.

You are given a list of schema updates, calls. Each item in calls is a list of strings where the first element is a case-sensitive tableName, followed by zero or more column names representing the ordered list of columns in the current snapshot: [tableName, column1, column2, ...]

For each snapshot call, return a list containing the finalized identifier assigned to each input column in that call, maintaining their original order.

Identifier Rules and Collision Resolution

  1. Dot Normalization: The initial candidate for an incoming column name is created by converting all period characters (.) into underscores (_).
  2. Table Isolation: Each table tracks its state independently.
  3. Case-Insensitive Uniqueness: No two distinct columns assigned to the same table can share the same case-insensitive identifier (e.g., A_B and a_b collide).
  4. Permanent Assignment: Once a column name is assigned an identifier in a table, that assignment is fixed forever. Even if the column is dropped, reordered, or reintroduced in later calls, its identifier never changes.
  5. Reserved Identifiers: Once an identifier is assigned, its lowercased string remains permanently reserved for that table. Dropped columns do not release their reserved identifiers.
  6. Collision Handling:
    • If an unassigned column's candidate identifier (converted to lowercase) is not yet reserved in that table, it takes that candidate identifier directly.
    • If the candidate identifier is already reserved, append _1, _2, _3, ..., attempting consecutive positive integer suffixes in increasing order until an unreserved candidate (case-insensitively) is found.
    • The final identifier preserves the character casing of the candidate (e.g., if candidate A_B collides twice, it receives A_B_2).
    • Natural suffixes in original names (like a_b_1) are treated as plain text. If a_b_1 collides, its next candidate is a_b_1_1 (not a_b_2).

Constraints

  • calls contains at most 1,000 sublists.
  • Total number of column occurrences across all calls is at most 10,000.
  • Names (table and column) consist of non-empty ASCII strings of at most 100 characters containing letters, digits, dots, and underscores.
  • Column names within a single input call are unique (exact match).
  • Table names are matched exactly (case-sensitive).

Notes

  • If a call contains only a table name with zero columns, return an empty list [] for that call.

Examples

Example 1

Input: rename_columns([['T', 'a.b', 'a_b', 'a_b_1'], ['T', 'a_b'], ['T', 'a_b_1', 'a.b', 'A.B'], ['U', 'a_b']])

Output: [['a_b', 'a_b_1', 'a_b_1_1'], ['a_b_1'], ['a_b_1_1', 'a_b', 'A_B_2'], ['a_b']]

Hints

Hint 1

Maintain two persistent data structures per table: a mapping from original column name to assigned identifier, and a set of all lowercased reserved identifiers.

Hint 2

When a column is seen for the first time, replace dots with underscores to form the base candidate.

Hint 3

Incrementally test candidate suffixes (_1, _2, etc.) against the lowercased reserved set until an available identifier is found, then store and return it.