Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 150/ 160
08. Data Quality, Governance, Security & AI Infrastructure

PII Detection, Masking, Tokenization & Anonymization

Detect PII and apply masking, tokenization, or anonymization appropriate to the use case.

AdvancedPII · Masking · Tokenization3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

PII, direct identifier, or quasi-identifier. Next, collect/retain only fields with purpose. Finally, vault access, re-identification testing, deletion, and audit.

Think of it like…

Masking is a redacted photocopy, tokenization is a claim ticket controlled by a locked vault, and anonymization is proving the released group cannot reasonably reveal the original person.

Where you’ve seen it

Store SSN as tok_9f... in the main table with format-preserving token only a narrowly granted fraud service can detokenize; analytical joins use HMAC-SHA256(secret_key, normalized_email), and public exports use grouped age bands.

Words you’ll hear

PII
Personal data that identifies or can identify a person, including direct IDs and sensitive combinations.
Masking
One-way display/derivation or controlled redaction that may be reversible only through another authorized system.
Tokenization
Replacement of sensitive data with a non-sensitive surrogate whose reverse mapping is separately secured.
Operational boundary
SHA-256(email) without salt is reversible by dictionary, partial masks can still identify users, and dropping direct IDs while preserving quasi-identifiers can expose people through linkage attacks. Classify at ingestion, use reversible tokenization through an access-controlled vault when identity reconciliation needs it, deterministic keyed HMAC for analytics, k-anonymity/differential privacy where appropriate, and validate residual linkage risk.

Visual diagram

Loading interactive diagram

Theory & deep dive

PII: the working mechanism

PII detection identifies personal or sensitive attributes; masking changes display/derived values, tokenization maps a secret to a vault-owned token, and anonymization removes re-identification risk for the chosen threat model.

PII, direct identifier, or quasi-identifier. Collect/retain only fields with purpose, then mask, tokenize, HMAC, aggregate, or anonymize; this exposes the state governed by Tokenization.

PII Detection, Masking, Tokenization & Anonymization in one concrete design

Store SSN as tok_9f... in the main table with format-preserving token only a narrowly granted fraud service can detokenize; analytical joins use HMAC-SHA256(secret_key, normalized_email), and public exports use grouped age bands.

Key takeaway: PII detection identifies personal or sensitive attributes

PII Detection, Masking, Tokenization & Anonymization: failure mode and production response

SHA-256(email) without salt is reversible by dictionary, partial masks can still identify users, and dropping direct IDs while preserving quasi-identifiers can expose people through linkage attacks.

Classify at ingestion, use reversible tokenization through an access-controlled vault when identity reconciliation needs it, deterministic keyed HMAC for analytics, k-anonymity/differential privacy where appropriate, and validate residual linkage risk. Track Salted/deterministic join as "use keyed HMAC, not raw unsalted hashes", and make k-anonymity visible to the dataset owner.

Key takeaway: Classify at ingestion, use reversible tokenization through an access-controlled vault when identity reconciliation needs it, deterministic keyed HMAC for analytics, k-anonymity/differential privacy where appropriate, and validate residual linkage risk.

How it works, step by step

Step 1 of 4
PII

1. Discover/classify

PII, direct identifier, or quasi-identifier. PII detection identifies personal or sensitive attributes; masking changes display/derived values, tokenization maps a secret to a vault-owned token, and anonymization removes re-identification risk for the chosen threat model.

PII, direct identifier, or quasi-identifier
Key numbers at this step
Salted/deterministic join
use keyed HMAC, not raw unsalted hashes
Masking
One-way display/derivation or controlled redaction that may be reversible only through another authorized system.

When to use it & trade-offs

What it is

PII detection identifies personal or sensitive attributes; masking changes display/derived values, tokenization maps a secret to a vault-owned token, and anonymization removes re-identification risk for the chosen threat model. Store SSN as tok_9f... in the main table with format-preserving token only a narrowly granted fraud service can detokenize; analytical joins use HMAC-SHA256(secret_key, normalized_email), and public exports use grouped age bands.

Why it matters: SHA-256(email) without salt is reversible by dictionary, partial masks can still identify users, and dropping direct IDs while preserving quasi-identifiers can expose people through linkage attacks. Classify at ingestion, use reversible tokenization through an access-controlled vault when identity reconciliation needs it, deterministic keyed HMAC for analytics, k-anonymity/differential privacy where appropriate, and validate residual linkage risk.

When to reach for it

  • Detect PII and apply masking, tokenization, or anonymization appropriate to the use case.
  • Choose PII because it provides this capability: Personal data that identifies or can identify a person, including direct IDs and sensitive combinations.
  • Keep evidence for Salted/deterministic join at "use keyed HMAC, not raw unsalted hashes" so the last production stage-Govern - vault access, re-identification testing, deletion, and audit-remains testable.

When to avoid or be careful

  • SHA-256(email) without salt is reversible by dictionary, partial masks can still identify users, and dropping direct IDs while preserving quasi-identifiers can expose people through linkage attacks.
  • Do not conflate Masking with PII. The second concept contributes different behavior: One-way display/derivation or controlled redaction that may be reversible only through another authorized system.
  • Release only after this guardrail is in place: Classify at ingestion, use reversible tokenization through an access-controlled vault when identity reconciliation needs it, deterministic keyed HMAC for analytics, k-anonymity/differential privacy where appropriate, and validate residual linkage risk.

PII / Masking / Tokenization - roles, limits, and operating cost

PII
Best for: Personal data that identifies or can identify a person, including direct IDs and sensitive combinations.

Strengths: Personal data that identifies or can identify a person, including direct IDs and sensitive combinations. PII detection identifies personal or sensitive attributes.

Watch out: SHA-256(email) without salt is reversible by dictionary, partial masks can still identify users, and dropping direct IDs while preserving quasi-identifiers can expose people through linkage attacks.

Masking
Best for: One-way display/derivation or controlled redaction that may be reversible only through another authorized system.

Strengths: One-way display/derivation or controlled redaction that may be reversible only through another authorized system. In this workflow it supports: Transform - mask, tokenize, HMAC, aggregate, or anonymize.

Watch out: The operating cost is real: each quasi-identifier group contains at least k records.

Tokenization
Best for: Replacement of sensitive data with a non-sensitive surrogate whose reverse mapping is separately secured.

Strengths: Replacement of sensitive data with a non-sensitive surrogate whose reverse mapping is separately secured. Its output can be checked with Token vault access.

Watch out: Operating guardrail: Classify at ingestion, use reversible tokenization through an access-controlled vault when identity reconciliation needs it, deterministic keyed HMAC for analytics, k-anonymity/differential privacy where appropriate, and validate residual linkage risk.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for PII, and how would Salted/deterministic join reveal it?”
  • “Why is Masking distinct from this behavior: replacement of sensitive data with a non-sensitive surrogate whose reverse mapping is separately secured?”
  • Production clue: SHA-256(email) without salt is reversible by dictionary, partial masks can still identify users, and dropping direct IDs while preserving quasi-identifiers can expose people through linkage attacks.
  • Design probe: Classify at ingestion, use reversible tokenization through an access-controlled vault when identity reconciliation needs it, deterministic keyed HMAC for analytics, k-anonymity/differential privacy where appropriate, and validate residual linkage risk.

Benchmarks & rules of thumb

Salted/deterministic join -
use keyed HMAC, not raw unsalted hashes
k-anonymity -
each quasi-identifier group contains at least k records
Token vault access -
detokenize calls and principals audited separately

Common mistakes & follow-ups

Common interview mistakes

  • SHA-256(email) without salt is reversible by dictionary, partial masks can still identify users, and dropping direct IDs while preserving quasi-identifiers can expose people through linkage attacks.
  • Reporting success from a job exit code without checking Salted/deterministic join and k-anonymity.
  • Treating this example as complete without replay, rollback, or access boundaries: Store SSN as tok_9f... in the main table with format-preserving token only a narrowly granted fraud service can detokenize; analytical joins use HMAC-SHA256(secret_key, normalized_email), and public exports use grouped age bands.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: Personal data that identifies or can identify a person, including direct IDs and sensitive combinations.
  • What does the recovery plan do when this occurs? SHA-256(email) without salt is reversible by dictionary, partial masks can still identify users, and dropping direct IDs while preserving quasi-identifiers can expose people through linkage attacks.
  • When is PII insufficient for k-anonymity, and what does adding Masking cost in Salted/deterministic join?
Interview tip: Lead with the data path and failure boundary: PII detection identifies personal or sensitive attributes; masking changes display/derived values, tokenization maps a secret to a vault-owned token, and anonymization removes re-identification risk for the chosen threat model. Then make the answer operational: Classify at ingestion, use reversible tokenization through an access-controlled vault when identity reconciliation needs it, deterministic keyed HMAC for analytics, k-anonymity/differential privacy where appropriate, and validate residual linkage risk.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply PII Detection, Masking, Tokenization & Anonymization with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Data Security: Encryption at Rest, Encryption in Transit & Key Management (KMS)
Next topic
Role-Based Access Control (RBAC) & Attribute-Based Access Control (ABAC)