Parse a card record string and validate the card number with the Luhn checksum

Quick Overview

Parse a semicolon-separated card record into the card number grouped in fours with dashes, a Luhn checksum verdict, and the holder's name written last name first. It tests careful string parsing, exact output formatting, and a correct right-to-left implementation of the Luhn check.

Parse a card record string and validate the card number with the Luhn checksum

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a string `data` that holds one credit card record in a custom format: ```text CARDNUM:XXXXYYYYZZZZWWWW;EXP:MMYY;NAME:FirstName LastName ``` Parse the string to extract the card number, the expiration date and the cardholder name, then validate the card number using the Luhn algorithm. Return a list of three strings: 1. The card number formatted with a dash every 4 digits, for example `"4539-1488-0343-6467"`. 2. `"VALID"` or `"INVALID"`, based on the Luhn checksum of the card number. 3. The cardholder name in `"LastName, FirstName"` format. The Luhn algorithm works as follows: starting from the rightmost digit, double every second digit. If doubling results in a number greater than 9, subtract 9. Sum all the digits. If the total modulo 10 is 0, the number is valid. ### Function Signature ```python def parse_and_validate_card(data: str) -> list[str]: ``` ### Rules - The fields always appear in the order `CARDNUM`, `EXP`, `NAME`, separated by `;`, and each label is followed by `:`. - The expiration date is part of the record but does not appear in the output. Do not compare it against any date. - In the Luhn check, the rightmost digit itself is not doubled. Doubling applies to the 2nd, 4th, 6th and later even positions, counting from the right. - The output strings must match exactly: uppercase `VALID` or `INVALID`, and a comma followed by one space between the last and first name. ### Constraints - `1 <= len(data) <= 200` - The card number portion always contains exactly 16 digits. - `EXP` is always in `MMYY` format with a valid month (`01` to `12`). - `NAME` always contains exactly one space, separating the first and last name. - The input string always follows the format `CARDNUM:...;EXP:...;NAME:...`. ### Examples ```text Input: data = "CARDNUM:4539148803436467;EXP:1225;NAME:John Doe" Output: ["4539-1488-0343-6467", "VALID", "Doe, John"] ``` Reading from the right, the doubled digits are 6, 6, 4, 0, 8, 1, 3, 4, which become 3, 3, 8, 0, 7, 2, 6, 8 (sum 37), and the other digits sum to 43. The total is 80, divisible by 10, so the card is valid. ```text Input: data = "CARDNUM:1234567890123456;EXP:0624;NAME:Alice Smith" Output: ["1234-5678-9012-3456", "INVALID", "Smith, Alice"] ``` The doubled digits contribute 32 and the others contribute 32, for a total of 64. Since 64 modulo 10 is 4, the checksum fails. The expiry `0624` has no effect on the output.

Overview: Parse a semicolon-separated card record into the card number grouped in fours with dashes, a Luhn checksum verdict, and the holder's name written last name first. It tests careful string parsing, exact output formatting, and a correct right-to-left implementation of the Luhn check.

Stripe logo
Stripe
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a string data that holds one credit card record in a custom format:

CARDNUM:XXXXYYYYZZZZWWWW;EXP:MMYY;NAME:FirstName LastName

Parse the string to extract the card number, the expiration date and the cardholder name, then validate the card number using the Luhn algorithm.

Return a list of three strings:

  1. The card number formatted with a dash every 4 digits, for example "4539-1488-0343-6467" .
  2. "VALID" or "INVALID" , based on the Luhn checksum of the card number.
  3. The cardholder name in "LastName, FirstName" format.

The Luhn algorithm works as follows: starting from the rightmost digit, double every second digit. If doubling results in a number greater than 9, subtract 9. Sum all the digits. If the total modulo 10 is 0, the number is valid.

Function Signature

def parse_and_validate_card(data: str) -> list[str]:

Rules

  • The fields always appear in the order CARDNUM , EXP , NAME , separated by ; , and each label is followed by : .
  • The expiration date is part of the record but does not appear in the output. Do not compare it against any date.
  • In the Luhn check, the rightmost digit itself is not doubled. Doubling applies to the 2nd, 4th, 6th and later even positions, counting from the right.
  • The output strings must match exactly: uppercase VALID or INVALID , and a comma followed by one space between the last and first name.

Constraints

  • 1 <= len(data) <= 200
  • The card number portion always contains exactly 16 digits.
  • EXP is always in MMYY format with a valid month ( 01 to 12 ).
  • NAME always contains exactly one space, separating the first and last name.
  • The input string always follows the format CARDNUM:...;EXP:...;NAME:... .

Examples

Input: data = "CARDNUM:4539148803436467;EXP:1225;NAME:John Doe"
Output: ["4539-1488-0343-6467", "VALID", "Doe, John"]

Reading from the right, the doubled digits are 6, 6, 4, 0, 8, 1, 3, 4, which become 3, 3, 8, 0, 7, 2, 6, 8 (sum 37), and the other digits sum to 43. The total is 80, divisible by 10, so the card is valid.

Input: data = "CARDNUM:1234567890123456;EXP:0624;NAME:Alice Smith"
Output: ["1234-5678-9012-3456", "INVALID", "Smith, Alice"]

The doubled digits contribute 32 and the others contribute 32, for a total of 64. Since 64 modulo 10 is 4, the checksum fails. The expiry 0624 has no effect on the output.