Mask Card Numbers in Free Text Using a Luhn Check
Last updated: November 29, 2026
Quick Overview
Given a line of free text from a bank statement or chat transcript, replace every standalone run of 13 to 19 digits that passes the Luhn checksum with asterisks, keeping only the last four digits. Leave other digit runs, such as order numbers, unchanged.
Cognizant
November 29, 20260
0
0 solved
Given a line of free text from a bank statement or chat transcript, replace every standalone run of 13 to 19 digits that passes the Luhn checksum with asterisks, keeping only the last four digits. Leave other digit runs, such as order numbers, unchanged.
Banking and financial services is Cognizant's largest segment, and PCI rules require that card numbers never land in logs or analytics stores unmasked. This screen-style task checks string handling, a small checksum algorithm, and the judgment to avoid masking every long number blindly.
What the Interviewer Expects
- Implements Luhn correctly, doubling every second digit from the right
- Matches whole digit runs only, not substrings of longer numbers
- Keeps the last four digits and the original length
- Leaves non-card digit runs intact
- Explains 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
- Card numbers often appear as 4111 1111 1111 1111 or with dashes. How do you extend the matcher?
- How would you test this against a corpus to measure false positives and misses?
- Where in a logging pipeline would you run this, and what if it is too slow?
- Would you ever mask a number that fails Luhn? When?
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 string. Output: the same string with each card-like number masked. Card-like means 13 to 19 consecutive digits, not touching another digit on either side, and a valid Luhn checksum. Masked ...
High-Level Design
Use a regex with lookarounds, (?<!\d)\d{13,19}(?!\d), so a 25-digit reference number is not partially matched. For each match, run Luhn: walk digits from the right, double every second digit, subtra...