Magicsheet logo

Masking Personal Information

Medium
89.4%
Updated 6/1/2025

Asked by 3 Companies

Topics

Masking Personal Information

What is this problem about?

The Masking Personal Information problem asks you to write a function that takes a string representing either an Email Address or a Phone Number. Your task is to mask (obfuscate) the string according to very specific formatting rules. For emails, you must lowercase everything, keep the first and last letters of the name, and replace the middle with *****. For phone numbers, you must strip all formatting, keep the last 4 digits visible, and mask the rest with ***-***-, including any international country codes.

Why is this asked in interviews?

This is a pure string manipulation and parsing problem. Companies ask it because sanitizing PII (Personally Identifiable Information) is a daily reality for backend engineers. It evaluates your attention to detail, your ability to handle multiple conditional formatting rules, and your familiarity with string libraries, regex, and character replacement.

Algorithmic pattern used

The problem does not require complex algorithms, but rather a Parsing and Conditional Formatting pattern.

  1. Determine the type: If the string contains an '@', it's an email. Otherwise, it's a phone number.
  2. For Emails: Split the string at the '@'. Convert the name part to lowercase, extract the first character and the last character, and concatenate them with ***** in between. Append the @ and the lowercase domain.
  3. For Phone Numbers: Strip all non-digit characters (+, -, (, ), ). Count the remaining digits. Take the last 4 digits. Prepend the local mask ***-***-. If the digit count is greater than 10, prepend the country code mask (e.g., +*- or +**-).

Example explanation

Email: "LeetCode@LeetCode.com"

  • Contains @. It's an email.
  • Split: Name "LeetCode", Domain "LeetCode.com".
  • Lowercase Name: "leetcode". First char 'l', last char 'e'.
  • Masked Name: "l*****e".
  • Lowercase Domain: "leetcode.com".
  • Result: "l*****e@leetcode.com".

Phone: "1(234)567-890"

  • No @. It's a phone.
  • Strip formatting: "1234567890".
  • Length is 10. (No country code).
  • Last 4 digits: "7890".
  • Result: "***-***-7890".

Common mistakes candidates make

A major pitfall is failing to properly strip all the distinct non-digit characters from the phone number. Using regex like s.replaceAll("[^0-9]", "") is the safest way to extract just the digits. Another common mistake is hardcoding index substrings for the phone number without counting the total digits first, causing out-of-bounds exceptions when international codes are present.

Interview preparation tip

For the Masking Personal Information coding problem, utilize your language's built-in string methods to save time. In Python, str.lower() and list comprehensions are great. In Java, StringBuilder and replaceAll("\\D", "") will handle the phone number cleanup cleanly. Always handle the structural split (Email vs Phone) at the very top of your function to keep the logic branches completely isolated.

Similar Questions