Build a Trust-Aware BIN Database
SquareSoftware Engineer · OnsiteLast updated
Build an in-memory card-prefix database that resolves brand and card type independently across overlapping writes. Practice combining longest-prefix matching with trust and recency rules while preserving omitted fields and supporting heavy lookup traffic.
Build a Trust-Aware BIN Database
Implement process_bin_operations(operations) for an in-memory database of card-number prefixes. A prefix identifies attributes such as card brand and card type. The function returns the result of every lookup operation, in order.
Each operation is one of:
("store", prefix, source, trust, brand, card_type)("lookup", card_number)
prefix, source, brand, and card_type are strings. For a store, at least one of brand or card_type is not None; the other may be None. trust is an integer. Every store is a new write, even when its source and values repeat.
For a lookup, resolve brand and card_type independently:
- Consider every stored prefix that is a prefix of
card_numberand that supplied the requested field. - Prefer the longest matching prefix.
- Among writes at that prefix, prefer the highest trust.
- If trust ties, prefer the most recent write.
A write that omits a field must not erase an older value for that field. Return each lookup as (brand, card_type), using None for a field with no matching write.
Constraints
1 <= len(operations) <= 200,000- Prefixes and card numbers contain only decimal digits.
1 <= len(prefix) <= 32;len(card_number) <= 64sourceis retained as write metadata; different sources may report the same values.- Inputs are valid; no format-validation code is required.
Example behavior
If a six-digit prefix supplies a high-trust brand but no type, and a more specific eight-digit prefix supplies only a type, a matching card number combines the six-digit brand with the eight-digit type. A later equal-trust write wins only for the field it supplies.
Candidate clarifications
Confirm field-by-field merging, longest-prefix precedence over trust, tie-breaking, whether duplicate values are allowed, and the required complexity for heavy lookup traffic.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.