Privacy-Enhancing Technologies and Anonymization Questions
Technical safeguards that reduce identifiability: anonymization, pseudonymization, tokenization, differential privacy, and related privacy-enhancing technologies. Covers the difference between anonymized and pseudonymized data, re-identification risk, and when each technique is appropriate. Includes evaluating the privacy-utility tradeoff of a given technical control.
Implement a PySpark script or provide Spark SQL that masks email addresses in a Parquet dataset 's3://company/events/' by replacing the local-part with a constant token while keeping the domain (for analytics that need domain-level data). The job must preserve partitioning, handle malformed addresses gracefully, and write to a new location atomically. Show key code or SQL and discuss performance considerations (UDF vs built-in functions).
Sample Answer
Approach: read Parquet, apply deterministic masking that replaces local-part (before @) with a constant token (e.g., "MASKED") while preserving domain; handle malformed emails by leaving them unchanged or placing in a null/malformed column; preserve existing partitioning and write atomically by writing to a temporary output prefix then performing an atomic rename (or use job-level commit via Hadoop FileOutputCommitter v2 / EMR S3-optimized committer). Prefer built-in string functions over Python UDFs for performance.
from pyspark.sql import SparkSession
from pyspark.sql.functions import expr, when, col
spark = SparkSession.builder.appName("mask-emails").getOrCreate()
src = "s3://company/events/"
dst_tmp = "s3://company/events_masked_tmp/"
dst_final = "s3://company/events_masked/"
# Read dataset (keeps partition columns)
df = spark.read.parquet(src)
# Masking using Spark SQL functions (no UDF)
# - If email matches basic pattern with '@' and domain contains '.', mask local-part
masked = df.withColumn(
"email_masked",
when(
expr("email IS NOT NULL AND email LIKE '%@%' AND instr(split(email,'@')[1], '.') > 0"),
expr("concat('***MASKED***','@', split(email,'@')[1])")
).otherwise(col("email"))
)
# Write to temp location preserving partitions (assumes original partition columns are known)
partition_cols = ["year","month","day"] # adjust as appropriate
(
masked
.write
.mode("overwrite")
.partitionBy(*partition_cols)
.parquet(dst_tmp)
)
# Atomic move: use filesystem rename if supported, otherwise rely on committer or run a job that writes directly with S3-optimized committer
fs = spark._jvm.org.apache.hadoop.fs.FileSystem.get(spark._jsc.hadoopConfiguration())
tmp_path = spark._jvm.org.apache.hadoop.fs.Path(dst_tmp)
final_path = spark._jvm.org.apache.hadoop.fs.Path(dst_final)
if fs.rename(tmp_path, final_path):
print("Atomic rename succeeded")
else:
raise RuntimeError("Rename failed; check committer configuration")
Key points:
- Use built-in functions (split, concat, instr, like) to leverage Spark SQL engine and avoid Python UDF serialization/exec overhead. Built-ins run in JVM and vectorized execution (Catalyst) giving much better perf and scalability.
- Handle malformed addresses via conditional logic; you can also write a separate column email_malformed_flag for QA.
- Preserve partitioning by using partitionBy with the same partition columns; reading original dataset first helps identify them.
- Atomic write: best practice is write to a temp prefix and rename; on S3 ensure you configure an S3-optimized committer (EMRFS or S3A with rename support) because S3 has eventual consistency and no true atomic rename—use job committers designed for object stores.
- Performance: avoid Python UDFs; if complex regex required, prefer Spark SQL regexp_replace/regexp_extract (JVM regex) rather than pandas/py UDFs. Use filter pushdown, predicate pruning, and repartition only if necessary to control shuffle.
- Edge cases: nulls, multiple @ signs, very long strings; add tests and logging for malformed rate.
Explain the difference between pseudonymization and anonymization. Give two practical examples where pseudonymization is appropriate for analytics and two examples where only anonymization would satisfy privacy requirements.
Sample Answer
Pseudonymization replaces direct identifiers (name, email, user_id) with reversible tokens or keys so records remain linkable to the same subject but cannot be directly read without the mapping. Anonymization irreversibly removes or transforms identifiers so reidentification is not reasonably possible. Key difference: pseudonymization preserves longitudinal/linkage utility at the cost of residual reidentification risk if the mapping or rich quasi-identifiers are exposed; anonymization prioritizes privacy by destroying linkability.
When pseudonymization is appropriate (analytics benefit from linking over time):
- Longitudinal cohort analysis for product engagement: replace user_id with a stable token so analysts can compute retention, churn, and funnels without access to real IDs.
- A/B experiments and personalization analytics: use hashed user tokens stored separately to attribute events across sessions while keeping the mapping protected.
When only anonymization is acceptable/required (no possibility of re-linking; high privacy bar):
- Public release of healthcare datasets for research where legal/regulatory requirements demand no reidentification risk — apply differential privacy or k-anonymity + noise and remove the mapping.
- Publishing mobility or location traces for open research: aggregate, add noise, and remove identifiers so individual trajectories cannot be reconstructed.
Best practices: treat pseudonymization as a security measure (protect keys, limit access), and use stronger anonymization or differential privacy when dataset sharing is external or reidentification risk must be minimized.
Propose a methodology to quantify re-identification risk for a dataset prior to sharing with an external analytics partner. Include metrics you would compute (uniqueness, sample re-identification probability), simulated attack models, and how you would decide on anonymization techniques (k-anonymity, l-diversity, t-closeness) or differential privacy.
Sample Answer
Goal: produce an operational, repeatable methodology a data engineering team can run before sharing datasets to quantify re-identification risk and choose mitigation.
- Clarify scope & assets
- List quasi-identifiers (QIDs), direct identifiers, sensitive attributes, dataset size, sampling fraction, and external linkage sources the partner might use.
- Compute risk metrics
- Uniqueness: fraction of records with unique QID combinations in the dataset and in population estimate (sample vs. population uniqueness).
- k-map: distribution of equivalence class sizes for chosen QIDs (min, median, % with k≤5).
- Sample Re-identification Probability (SRP): for each record, SRP = 1 / (estimated population equivalence class size) × sampling_fraction_adjustment.
- Prosecutor/Journalist/Marketer risk variants: conditional probabilities under different attacker goals.
- Attribute disclosure risk: for each equivalence class, entropy or leakage of sensitive attribute (use l-diversity and t-closeness statistics).
- Confidence intervals via bootstrap to express uncertainty.
- Simulated attack models
- Linkage attack: simulate linking against likely external data (voter rolls, commercial data) using exact and fuzzy matching; measure matches and false positives.
- Inference attack: build classifiers predicting sensitive attributes from QIDs; measure AUC and calibration.
- Record linkage under noise: allow perturbations (typos, missingness).
- Composition attack: combine multiple releases or auxiliary datasets.
- Decision framework for mitigation
- If many small equivalence classes or SRP > acceptable threshold (e.g., 0.05), apply anonymization.
- Choose technique by trade-offs:
- k-anonymity: when preventing identity disclosure is primary and utility can tolerate generalization; pick k (≥5–20) based on risk tolerance.
- l-diversity: if attribute distribution within classes is skewed (sensitive attr dominance); require l (e.g., 3–5).
- t-closeness: if preserving distribution of sensitive attribute is critical; set t per domain.
- Differential privacy (DP): when strong, provable bounds against arbitrary auxiliary info are required—prefer DP for aggregate queries or synthetic data; choose ε based on privacy budget and utility testing (e.g., start ε∈[0.1,1] for high privacy, test).
- Hybrid: use microaggregation/k-anonymity pre-processing then add DP noise for analytics outputs.
- Validate & iterate
- Re-run risk metrics and simulated attacks after anonymization; quantify utility loss (statistical/ML metrics).
- Produce a risk-utility report with recommended parameters, assumed attacker models, and residual SRP.
- Operationalize
- Automate as a pipeline (Spark) producing reproducible reports, retain parameter logs, and enforce policy thresholds before sharing.
This methodology gives quantifiable metrics, attack simulations, and a principled way to pick anonymization balancing risk and analytical utility.
As a data engineer, outline approaches to store and query PII attributes in a dimension while enabling analytics and complying with privacy requirements: consider hashing, tokenization with a vault, reversible encryption, and separate pseudonymized analytics dims. Discuss trade-offs for joins, performance, and re-identification risk.
Sample Answer
Situation: You need to store/query PII in a dimension so analysts can join and run analytics while meeting privacy/regulatory constraints.
High-level options and trade-offs:
- Hashed PII (one-way, deterministic)
- How: Store H = hash(PII + salt). Use keyed HMAC or SHA-256 with a secret salt.
- Joins: Deterministic hashes allow joins across systems if same salt/key used.
- Performance: Fast, minimal storage overhead.
- Re-identification risk: High if PII domain is small (emails, phone numbers) — susceptible to brute force / rainbow table attacks unless salt is long/secret and rate limits applied.
- Use when: Low-risk linking required, and brute-force mitigations exist.
- Tokenization with vault (non-reversible in DB)
- How: Replace PII with opaque token issued by a vault (Deterministic or random tokens). Vault stores mapping and enforces access controls/audit.
- Joins: Deterministic tokenization supports joins; random tokens do not.
- Performance: Slight latency at ingestion to call vault; tokens are small and fast in queries.
- Re-identification risk: Low in DB (vault needed to resolve). Vault compromise is central risk.
- Use when: Strong governance, need central control and audit of re-identification.
- Reversible encryption (deterministic vs non-deterministic)
- How: Use AEAD (e.g., AES-GCM) with KMS-managed keys. Deterministic encryption (ECB-like) allows equality joins; randomized (with IV) prevents joins.
- Joins: Deterministic = possible joins; Non-deterministic = cannot join on ciphertext.
- Performance: Crypto overhead at read/write; can precompute encrypted join keys to speed queries.
- Re-identification risk: Moderate — protected by KMS and access controls; key compromise allows decryption.
- Use when: Need to query but must maintain confidentiality; enforce strict key policy and rotation.
- Separate pseudonymized analytics dims (surrogate keys + linkage table)
- How: Keep a tight, access-controlled identity store mapping PII ↔ internal ID; analytics dims contain only surrogate ID and non-PII attributes.
- Joins: Analysts join on surrogate ID — fast and simple.
- Performance: Excellent for large-scale analytics; avoids crypto on query path.
- Re-identification risk: Low in analytics layer; identity store is high-value and must be heavily protected/audited.
- Use when: Clear separation of identity and analytics; supports scale and minimizes exposure.
Cross-cutting considerations and recommendations:
- Prefer central identity vault (tokenization or identity store) + pseudonymized analytics dims for best balance of queryability and risk.
- Use deterministic crypto/tokenization only when you need equality joins; otherwise prefer randomized encryption or one-way hashes.
- Key management: Use cloud KMS, strict IAM, key rotation, and isolated key custodianship.
- Minimize PII exposure in compute (use enclave/secure processing for re-identification), audit all de-tokenization, and enforce row/column-level access controls and query logging.
- Consider additional protections: rate limiting, differential privacy/aggregation, and retention policies.
Example pattern: Ingest raw PII into a secure staging VPC -> tokenize via vault (deterministic token for email) -> store token in analytics dim with surrogate ID and signals -> analysts join on token/surrogate. Vault holds mapping and is the only component allowed to re-identify under strict approval and auditing.
Discuss the feasibility and trade-offs of using homomorphic encryption or multi-party computation (MPC) for analytics workloads at the scale of tens to hundreds of millions of users. For what classes of analytics are these techniques practical today, and what hybrid architectures might make them more usable?
Sample Answer
Situation: You're evaluating whether to run analytics over tens-to-hundreds-of-millions of users with cryptographic privacy-preserving tech (homomorphic encryption (HE) or multi-party computation (MPC)) and need practical guidance.
Feasibility and trade-offs
- Performance: Fully homomorphic encryption (FHE) offers arbitrary computation on ciphertexts but remains orders of magnitude slower and memory-heavy compared to plaintext. Practical throughput for large-scale aggregate workloads is limited; per-record HE operations are CPU and memory intensive, making naïve per-user encryption at 100M+ infeasible in reasonable cost/latency.
- Accuracy & expressiveness: Some HE schemes support exact integer arithmetic or approximate (CKKS) for floats; complex analytics (joins, heavy ML training) are awkward or expensive.
- Security model: MPC requires multiple non-colluding parties and interactive protocols; latency grows with rounds. MPC avoids trusting a single party but needs coordination, bandwidth, and specialized orchestration.
- Cost & engineering: Both require specialized libraries, careful parameter selection, and expert cryptography review. Operational complexity (key management, scaling, monitoring) increases.
Classes of analytics practical today
- Simple aggregations: sums, counts, histograms, distinct counts (with sketches) — especially when done as batched, small-windowed operations.
- Linear analytics and statistics: mean, variance, simple regressions using HE (CKKS) or MPC secure aggregation.
- Private set intersection (PSI) and cardinality estimation at scale — well-studied, optimized protocols exist.
- Model inference with small models or when using secure enclaves; large-scale training remains mostly impractical purely under HE/MPC.
Hybrid architectures to increase usability
- Local pre-aggregation + HE/MPC: Each client computes local summary (e.g., histogram, sketch, gradients) and sends compact encrypted/secret-shared payloads. This reduces work and network load.
- Threshold MPC + sharding: Split data across multiple data holders and run MPC on aggregated shards rather than per-user records; combine with secure aggregation to reduce rounds.
- HE for non-interactive, low-compute ops + MPC for joint steps: Use HE for single-round sums/averages and MPC for operations requiring interaction/complex logic.
- Trusted Execution Environments (TEEs) as an accelerator: Use TEEs (e.g., Nitro, SGX) to perform heavy computation inside enclaves, with HE/MPC used for bootstrapping or verification — trading some trust assumptions for practicality.
- Federated analytics orchestration: Orchestrate client-side computation with secure aggregation servers; use differential privacy before release to reduce cryptographic burden.
Practical recommendations for a data engineer
- Start with threat model: identify which parties must not learn raw data and what exact outputs are acceptable.
- Prefer hybrid designs: push aggregation and compression to clients, use MPC/HE only for the minimized sensitive operations.
- Prototype with representative data and measure CPU, memory, network, and latency; use existing libraries (Microsoft SEAL, PALISADE, MP-SPDZ) and vetted MPC providers.
- Apply differential privacy on final results to limit leakage and reduce cryptographic strictness.
- Monitor cost & ops complexity; expect higher cloud bills and need for cryptographic expertise.
Bottom line: For tens to hundreds of millions of users, full HE or full MPC end-to-end is currently impractical for complex analytics. Practical use today focuses on simple aggregates, PSI/PSU, and hybrid architectures that minimize cryptographic workload by combining client-side summarization, secure aggregation, TEEs, and differential privacy.
Unlock Full Question Bank
Get access to all 9 Privacy-Enhancing Technologies and Anonymization interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.