Mask Card Numbers in Authorization Logs Using Luhn Checks

Last updated: November 29, 2026

Quick Overview

Given a raw log line from an authorization service, replace every substring that is a valid payment card number (15-digit Amex numbers starting 34 or 37, or 16 to 19 digit numbers) with asterisks except the last four digits. Digit runs that fail the Luhn check, like order ids, must be left alone, and numbers may contain spaces or hyphens.

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

0

0

0 solved


Given a raw log line from an authorization service, replace every substring that is a valid payment card number (15-digit Amex numbers starting 34 or 37, or 16 to 19 digit numbers) with asterisks except the last four digits. Digit runs that fail the Luhn check, like order ids, must be left alone, and numbers may contain spaces or hyphens.

A warm-up coding question in the style of an American Express phone screen for payments teams, where PCI DSS means a full card number must never land in a log. The interviewer checks clean string handling, a correct Luhn implementation, and whether you avoid masking things that only look like card numbers.

What the Interviewer Expects
  • Implements Luhn correctly, doubling every second digit from the right
  • Knows the Amex format: 15 digits with a 34 or 37 prefix
  • Handles separators such as spaces and hyphens inside the number
  • Avoids false positives on long numeric ids that fail Luhn
  • Keeps only the last four digits visible
Key Topics to Cover
Luhn checksum
Regular expressions
PCI DSS
String processing
Data masking
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 stop this from becoming a bottleneck on a log pipeline doing millions of lines a minute?
  • Where in the stack should masking really happen so a raw number never reaches the logger at all?
  • How would you handle a number split across two log fields or lines?
  • What would you do about tokenized values that also pass Luhn?
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 one log line. Output is the same line with each valid card number masked to * characters plus the last four digits. A candidate is 15 to 19 digits, optionally separated by single spaces or ...

High-Level Design
  1. Use a regex to find digit runs with optional single separators, bounded so they are not part of a longer number.
  2. Strip separators and validate prefix, length and Luhn.
  3. Replace valid matches w...

Submit Your Answer
Markdown supported

Related Questions