PII Redactor
Before customer data touches a log line — or a third-party LLM — the personal bits have to go. Mask emails, SSNs, credit cards, and phone numbers with pattern matching, leaving the rest readable. It’s the redaction step that keeps you out of a compliance incident. Solve it in Python or TypeScript, with hidden tests.
The problem
Implement redact(text): replace personal data with typed placeholders, in this order — emails → [EMAIL], US SSNs (###-##-####) → [SSN], 16-digit credit cards (optionally space/dash grouped) → [CARD], and phone numbers (###-###-#### or ###.###.####) → [PHONE]. Everything else is left untouched. Return the redacted string.
text = 'mail a@b.com ssn 123-45-6789 ph 555-123-4567'mail [EMAIL] ssn [SSN] ph [PHONE]- Apply the patterns in the given order (email, SSN, card, phone) so a card isn’t mis-tagged as a phone.
- Replace with the typed token (
[EMAIL],[SSN],[CARD],[PHONE]) — keep the surrounding text. - Non-matching text passes through unchanged.
Your turn — write it
Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.
Implement redact(text) with four ordered regex replacements. Use word boundaries so you match whole tokens, and apply email first (its "@" is unambiguous) and card before phone (a 16-digit run must not be split into phone-shaped pieces).
- Email:
[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}→[EMAIL]. - SSN:
\b\d{3}-\d{2}-\d{4}\b→[SSN]. - Card:
\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b→[CARD](before phone). - Phone:
\b\d{3}[-.]\d{3}[-.]\d{4}\b→[PHONE].
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.