Katkat

How would you improve card-type detection?

Capital OneMachine Learning Engineer · Onsite

Last updated

This question tests your ability to design a modular and maintainable classification system that identifies credit card networks (e.g., Visa, Mastercard) based on raw input data while considering validation and error handling.

Problem Statement

You are tasked with building a service that determines the card network (e.g., Visa, Mastercard, American Express) from a given credit card number string.

Implement a class CardClassifier with a method classify(card_number: str) -> str. You must rely on the Industry Standard: the first digits of a card number, known as the Issuer Identification Number (IIN) or Bank Identification Number (BIN).

Requirements

  1. Implement the detection logic using the following rules:
    • Visa: Starts with '4'
    • Mastercard: Starts with '51'-'55' or '2221'-'2720'
    • American Express: Starts with '34' or '37'
    • Discover: Starts with '6011', '644'-'649', or '65'
  2. Include a basic validation check (e.g., ensure the input is numeric and has a reasonable length, such as 13-19 digits).
  3. Return "Unknown" if the card network cannot be identified or if the input is invalid.

Example

classifier = CardClassifier()
classifier.classify("4111111111111111") # Returns "Visa"
classifier.classify("341234567890123")    # Returns "American Express"
classifier.classify("12345")             # Returns "Unknown"

Constraints

  • The input string may contain spaces or hyphens that should be stripped before processing.
  • Efficiency is important; the classification should run in O(L) time where L is the length of the string.

Hints

Hint 1

Start by normalizing the input string by removing non-digit characters.

Hint 2

Consider using a prefix-matching approach or a Trie data structure if the number of card ranges grows significantly.

Hint 3

Always validate the length of the string before attempting to check prefix patterns to avoid index errors.

Follow-up questions

  • How would you modify this system if the card network rules were stored in an external database and changed frequently?
  • How would you implement the Luhn Algorithm to verify if the credit card number is mathematically valid before attempting to classify it?
  • If this system were deployed in a high-traffic environment, how would you handle logging and monitoring for 'Unknown' classification rates?