Mask Luhn-Valid Card Numbers Inside Unstructured Document Text
Last updated: November 29, 2026
Quick Overview
Write a function that scans free-form text, finds runs of 13 to 19 digits that may be separated by single spaces or hyphens, and replaces every digit except the last four with '*' only when the digit run passes the Luhn checksum, leaving all other text unchanged.
Concentric Ai
November 29, 20260
0
0 solved
Write a function that scans free-form text, finds runs of 13 to 19 digits that may be separated by single spaces or hyphens, and replaces every digit except the last four with '*' only when the digit run passes the Luhn checksum, leaving all other text unchanged.
PCI data hidden in contracts, tickets and spreadsheets is one of the categories a data security platform like Concentric AI must find and redact. This online-assessment style task checks regex boundaries, a correct checksum, and preserving the original formatting while masking.
What the Interviewer Expects
- Implement the Luhn checksum correctly from the rightmost digit
- Allow space or hyphen separators without matching across longer digit runs
- Keep separators and non-card text byte for byte
- Leave Luhn-invalid numbers such as order ids untouched
- Explain why the checksum reduces false positives
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
- How would you also use IIN prefixes (4, 51 to 55, 34/37) to cut false positives further?
- How would you avoid masking a card-like number inside a longer serial number?
- What would you report to a dashboard instead of the raw digits?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Find candidate card numbers: 13 to 19 digits, optionally separated by a single space or hyphen between digits, not glued to other digits or hyphens. Mask only candidates whose digits pass Luhn, keepin...
High-Level Design
Use a regex with lookarounds (?<![\d-]) and (?![\d-]) so we never match the middle of a longer number. Pass a replacement function to re.sub: strip separators, verify length and Luhn, then rebui...