KatInterview

Card Prefix Registry with Per-Field Trust Resolution

Data Structure DesignModel answer
MediumModel answerData Structure Designhash-maplongest-prefix-matchtrie

Last updated

Design an in-memory registry mapping digit prefixes to brand and card type, resolving each field by longest prefix, then trust, then recency, with fast lookups.

Card Prefix Registry with Per-Field Trust Resolution

You are building an in-memory registry that maps leading digits of payment card numbers to descriptive attributes. Write a function process_bin_operations(operations) that executes a list of operations in order and returns the answers to all lookups, in the order they appeared.

Operations

Each operation is a tuple of one of two shapes:

  • ("store", prefix, source, trust, brand, card_type) records a new report about a prefix.
  • ("lookup", card_number) asks for the brand and card type of a full card number.

For a store, prefix, source, brand and card_type are strings (the last two may be None), and trust is an integer. At least one of brand and card_type is non-None. Every store counts as a brand-new report, even if an earlier store had the same source and values.

Resolution rules for a lookup

brand and card_type are resolved separately. For each one:

  1. Collect every stored prefix that is a leading substring of card_number and whose reports supplied a value for that field.
  2. Choose the longest such prefix.
  3. Among the reports at that prefix that supplied the field, choose the one with the highest trust.
  4. If trust is tied, choose the most recently stored report.

A report that leaves a field as None must never overwrite or hide an older value for that field. A lookup returns (brand, card_type), using None for any field with no qualifying report.

Constraints

  • 1 <= len(operations) <= 200,000
  • Prefixes and card numbers consist of decimal digits only.
  • 1 <= len(prefix) <= 32, len(card_number) <= 64
  • source is just metadata; different sources may report identical values.
  • All inputs are valid.

Example

Suppose a 6-digit prefix reports a high-trust brand with no card type, and a longer 8-digit prefix, which the card number also starts with, reports only a card type. Looking up that card yields the brand from the 6-digit prefix and the type from the 8-digit one. If a later report at the same prefix has equal trust, it replaces the earlier value only for the field(s) it actually supplies.

What to cover

Explain your data structure and why it handles heavy lookup traffic, state the time and space complexity, and write the code. Mention the edge cases you would test.

Hints

Hint 1

Because writes are never deleted, you do not need to keep every report. For each (prefix, field) pair, only the current best report can ever matter.

Hint 2

A new report is always the most recent one so far. So at a given prefix and field it wins exactly when its trust is greater than or equal to the stored winner's trust, and a None field simply skips the update.

Hint 3

Stored prefixes are at most 32 digits, so a lookup only needs to check at most 32 candidate prefixes of the card number, from longest to shortest. Fill each field from the first prefix that has it and stop when both are filled.