Mask Card Numbers in Authorization Logs Using Luhn Validation
Last updated: November 29, 2026
Quick Overview
Write a function that scans a free-text log line, finds every 13 to 19 digit sequence (optionally grouped by spaces or dashes) that passes the Luhn checksum, and replaces all but its last four digits with asterisks while leaving other numbers untouched.
JPMorgan Chase
November 29, 20260
0
0 solved
Write a function that scans a free-text log line, finds every 13 to 19 digit sequence (optionally grouped by spaces or dashes) that passes the Luhn checksum, and replaces all but its last four digits with asterisks while leaving other numbers untouched.
J.P. Morgan Payments and Chase card services must keep primary account numbers out of logs to stay PCI DSS compliant, and leaked PANs in debug output are a real audit finding. A phone screen version checks string handling, the Luhn algorithm, and whether you avoid masking order ids that merely look long.
What the Interviewer Expects
- Implements Luhn correctly, doubling every second digit from the right
- Uses digit boundaries so a PAN is not matched inside a longer number
- Preserves separators and only replaces digits
- Leaves non-Luhn numbers alone to avoid destroying useful ids
- Discusses false positives and where in the pipeline masking belongs
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- A 16 digit order id happens to pass Luhn. How would you reduce false positives (BIN ranges, keywords)?
- How would you make this fast enough to run on every log line in a high-volume authorization service?
- Where should masking happen: in the application logger, the log shipper, or the indexer?
- How would you also catch PANs that are split across two JSON fields?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: one log line. Output: the same line with each candidate PAN masked as ************1111. A candidate is 13 to 19 digits, possibly grouped with single spaces or dashes, not touching other digit...
High-Level Design
- A regex finds candidate runs: a digit followed by 12 to 18 more digits, each optionally preceded by one space or dash, with lookarounds
(?<!\d)and(?!\d)so we never cut a longer number. - Fo...