Validate Card Numbers and Detect the Card Brand

Last updated: November 29, 2026

Quick Overview

Given a raw card number string that may contain spaces or dashes, return its brand (visa, mc, amex), "unknown" for a valid number with no matching brand, or "invalid" if it fails format or Luhn checks.

Adyen
Coding & Algorithms
Software Engineer
Adyen
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given a raw card number string that may contain spaces or dashes, return its brand (visa, mc, amex), "unknown" for a valid number with no matching brand, or "invalid" if it fails format or Luhn checks.

A warm-up in the style of an Adyen backend phone screen, close to what the checkout and card-processing code does before a request ever reaches an issuer. The interviewer checks clean string handling, a correct Luhn implementation, and longest-prefix brand matching rather than a chain of if statements.

What the Interviewer Expects
  • Normalise the input by stripping spaces and dashes before validating
  • Implement the Luhn checksum correctly, doubling every second digit from the right
  • Match brands by longest prefix and check the allowed length for that brand
  • Keep brand rules in data so new ranges can be added without code changes
  • Discuss why the full number should never be logged
Key Topics to Cover
Luhn checksum
BIN prefix matching
Input normalisation
PCI data handling
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you support thousands of BIN ranges loaded from a file and keep lookup fast?
  • Some cards are co-branded (for example a local scheme plus Visa). How would you return both?
  • How would you mask the number for logs and receipts?
  • Where in a payment flow should this check run, client side, server side, or both?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input is a string with digits plus optional spaces or dashes. Output one of visa, mc, amex, unknown, invalid. Valid means 12 to 19 digits and a passing Luhn checksum. Brand rules: Amex start...

High-Level Design

Two independent steps: a structural and checksum validation, then a brand lookup. I keep the brand table as data (brand, prefixes, allowed lengths) and pick the longest matching prefix whose length ru...


Submit Your Answer
Markdown supported

Related Questions