← Questions

Card number validation with the Luhn check

0
StripeCodingLast asked Sep 2025

A coding interview question asked in Stripe's HackerRank assessment, last asked Sep 2025. It tests strings, validation and checksums.

Build a set of payment-card utilities around the Luhn checksum and three card networks:

  • VISA: 16 digits, starts with 4.
  • MASTERCARD: 16 digits, starts with 51 to 55.
  • AMEX: 15 digits, starts with 34 or 37.

Luhn check: going from the rightmost digit leftward, double every second digit and subtract 9 from any result above 9; the number is valid if the sum of all digits is divisible by 10.

Part 1: validate and identify

classify(number: str) -> str returns "INVALID_CHECKSUM" if the number fails Luhn, else its network. A Luhn-valid number that matches no network also returns "INVALID_CHECKSUM" in this part.

Part 2: unknown networks

Change Part 1 so a Luhn-valid number that matches no network returns "UNKNOWN".

Part 3: wildcards

The input may contain *, each standing for one unknown digit. count_matches(pattern: str) -> list[str] returns how many Luhn-valid numbers of each network the pattern can be, as "NETWORK,count" in the order VISA, MASTERCARD, AMEX, leaving out zero counts. Count without trying every completion.

Part 4: one error

The input ends with ?, meaning exactly one error happened to the digits before it: a digit was changed, a digit was removed, a digit was added, or two adjacent digits were swapped. recover(observed: str) -> list[str] returns every original number that was Luhn-valid and belonged to one of the three networks and could have produced the input with exactly one such error, as "NUMBER,NETWORK", sorted.

Example

classify("4242424242424242")   ->  "VISA"
classify("378282246310005")    ->  "AMEX"
classify("4242424242424241")   ->  "INVALID_CHECKSUM"
classify("6011111111111117")   ->  "INVALID_CHECKSUM"   # Part 2: "UNKNOWN"

count_matches("42424242424242**")  ->  ["VISA,10"]
count_matches("*555555555554444")  ->  ["MASTERCARD,1"]

recover("3782822463100055?")  ->  ["378282246300055,AMEX",
                                   "378282246310005,AMEX",
                                   "378282463100055,AMEX"]

Constraints

  • Inputs contain only digits, plus * in Part 3 and one trailing ? in Part 4.
  • Part 3 patterns can have many wildcards, so counts can be very large.

Clarifying questions

  • Should a wrong length fail the network check only, or the whole validation?
  • In Part 3, are wildcards allowed in the network prefix?
  • In Part 4, can the original be the observed string itself?
  • What output order is expected when several networks match?

What a strong answer covers

  • One Luhn helper and one network helper reused by every part.
  • Part 3: each free digit position maps the 10 digits onto the 10 residues mod 10, so with f free wildcards exactly 10^(f-1) completions pass Luhn; wildcards in the prefix are limited to the network's allowed values and multiply the count; with no free wildcard, check each prefix fill directly.
  • Part 4: generating every inverse edit (change, insert, delete, swap), deduplicating with a set and filtering by Luhn and network.
  • Tests from known test card numbers and from edits applied to them.

Follow-ups

  • Add more networks with prefix ranges (2221 to 2720 for MASTERCARD) and several lengths.
  • Part 3 with several thousand wildcards: how do you return the count?
  • Part 4 with up to two errors. How fast does the candidate set grow?

Where it's asked

What it tests

strings · validation · checksums

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding