Coding questions · Square
Build a Trust-Aware BIN Database
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.
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.
Build a Trust-Aware BIN Database
Implement process_bin_operations using six parallel operation arrays. A store row supplies a nonempty digit prefix, source, signed-32 trust, and at least one nonempty brand or card_type. The empty string is reserved as the omission sentinel and cannot be a stored brand or card type. A lookup row uses keys[i] as the card number. Resolve brand and type independently by longest matching prefix, then highest trust at that prefix, then most recent equal-trust write. Return [brand, card_type] pairs for lookups, using the reserved empty string for a missing field.
Hints
- Maintain the trusted winner separately for each field and exact prefix.
- During lookup, search prefix lengths from longest to shortest for each unresolved field.
Approach
For every exact prefix, keep the winning brand write and type write separately. A new nonempty field replaces its winner only with greater trust or equal trust at a later operation. Lookup scans prefixes from most specific to least and stops searching a field as soon as its longest stored winner is found.
- Time complexity: O(p) per store and O(L^2) character copying per lookup in this direct substring implementation, with L at most 64.
- Space complexity: O(w) for field winners and lookup results.