Bird
Raised Fist0
ML Pythonml~20 mins

Privacy considerations in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Privacy Protector
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Differential Privacy
Which of the following best describes the main goal of differential privacy in machine learning?
ATo speed up the training process by reducing data size
BTo prevent the model from revealing whether any single individual's data was used during training
CTo ensure that the model's predictions are always 100% accurate on training data
DTo increase the complexity of the model to avoid overfitting
Attempts:
2 left
💡 Hint
Think about protecting individual data privacy when training models.
❓ Predict Output
intermediate
2:00remaining
Output of Privacy-Preserving Noise Addition
What is the output of the following Python code that adds Laplace noise to a data point for privacy?
ML Python
import numpy as np
np.random.seed(0)
data_point = 10
noise = np.random.laplace(loc=0.0, scale=1.0)
private_data = data_point + noise
print(round(private_data, 2))
A11.43
B11.76
C11.54
D11.65
Attempts:
2 left
💡 Hint
Run the code to see the exact noise value added.
❓ Model Choice
advanced
2:00remaining
Choosing a Privacy-Preserving Model Technique
You want to train a machine learning model on sensitive health data while ensuring privacy. Which technique is best suited for this?
AUse federated learning to train models locally on devices without sharing raw data
BTrain a deep neural network on raw data without modifications
CCollect all data centrally and anonymize by removing names only
DUse a simple linear regression without any privacy measures
Attempts:
2 left
💡 Hint
Think about methods that keep data on user devices.
❓ Metrics
advanced
2:00remaining
Evaluating Privacy-Utility Tradeoff
In a privacy-preserving model, which metric combination best reflects a good balance between privacy and model usefulness?
AHigh epsilon value in differential privacy and low model accuracy
BHigh epsilon value in differential privacy and high model accuracy
CLow epsilon value in differential privacy and high model accuracy
DLow epsilon value in differential privacy and low model accuracy
Attempts:
2 left
💡 Hint
Lower epsilon means stronger privacy.
🔧 Debug
expert
3:00remaining
Debugging Privacy Leakage in Model Training
You trained a model with differential privacy but found it still leaks some private info. Which code snippet below is the cause?
ML Python
def train_model(data, epsilon):
    # Missing noise addition step
    model = SomeModel()
    model.fit(data)
    return model
AThe model uses federated learning, which always leaks data
BThe model uses too much noise, causing privacy leakage
CThe model trains on encrypted data, which causes leakage
DThe model does not add noise to gradients or parameters, so privacy is not enforced
Attempts:
2 left
💡 Hint
Differential privacy requires adding noise during training.

Practice

(1/5)
1. What is the main reason to consider privacy in machine learning?
easy
A. To protect personal data from being exposed
B. To make the model run faster
C. To increase the size of the dataset
D. To improve the color of data visualizations

Solution

  1. Step 1: Understand the role of privacy

    Privacy in machine learning is about keeping personal data safe and secure.
  2. Step 2: Identify the main goal

    The main goal is to protect personal data from being exposed or misused.
  3. Final Answer:

    To protect personal data from being exposed -> Option A
  4. Quick Check:

    Privacy = Protect personal data [OK]
Hint: Privacy means keeping personal data safe [OK]
Common Mistakes:
  • Thinking privacy speeds up models
  • Confusing privacy with data size
  • Mixing privacy with visualization features
2. Which of the following is a correct way to protect privacy before training a model?
easy
A. Include all personal details in the dataset
B. Remove or mask personal identifiers from data
C. Share the raw data publicly
D. Use the same password for all data files

Solution

  1. Step 1: Review data preparation for privacy

    Removing or masking personal identifiers helps hide sensitive information.
  2. Step 2: Check options for privacy protection

    Including personal details or sharing raw data exposes privacy; using weak passwords is unsafe.
  3. Final Answer:

    Remove or mask personal identifiers from data -> Option B
  4. Quick Check:

    Mask personal data = Protect privacy [OK]
Hint: Mask or remove personal info before training [OK]
Common Mistakes:
  • Keeping all personal details
  • Sharing raw data openly
  • Ignoring data security practices
3. Consider this Python code snippet for data anonymization:
def anonymize(data):
    return [{k: (v if k != 'name' else 'REDACTED') for k, v in record.items()} for record in data]

sample_data = [{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]
print(anonymize(sample_data))
What is the output?
medium
A. [{'name': 'REDACTED', 'age': 30}, {'name': 'REDACTED', 'age': 25}]
B. SyntaxError
C. [{'age': 30}, {'age': 25}]
D. [{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]

Solution

  1. Step 1: Understand the anonymize function

    The function replaces the value of the 'name' key with 'REDACTED' for each record.
  2. Step 2: Apply the function to sample_data

    Names 'Alice' and 'Bob' become 'REDACTED', ages remain unchanged.
  3. Final Answer:

    [{'name': 'REDACTED', 'age': 30}, {'name': 'REDACTED', 'age': 25}] -> Option A
  4. Quick Check:

    Names replaced with REDACTED = [{'name': 'REDACTED', 'age': 30}, {'name': 'REDACTED', 'age': 25}] [OK]
Hint: Check key replacement in dict comprehension [OK]
Common Mistakes:
  • Thinking names stay the same
  • Removing the 'name' key instead of replacing
  • Confusing syntax causing errors
4. The following code is intended to remove personal emails from a dataset but has a bug:
def remove_emails(data):
    for record in data:
        if 'email' in record:
            record.pop('email')
    return data

sample = [{'name': 'John', 'email': 'john@example.com'}, {'name': 'Jane'}]
print(remove_emails(sample))
What is the bug and how to fix it?
medium
A. Loop should iterate over a copy of data to avoid modification errors
B. pop() should be pop('email', None) to avoid errors
C. No bug; code works correctly
D. Function should return a new list without emails instead of modifying in place

Solution

  1. Step 1: Analyze the pop usage

    pop('email') removes the 'email' key if it exists; no error if checked first.
  2. Step 2: Check the loop and data modification

    Loop modifies records in place safely; no iteration over changing list structure.
  3. Final Answer:

    No bug; code works correctly -> Option C
  4. Quick Check:

    pop after key check = safe removal [OK]
Hint: Check if key exists before pop to avoid errors [OK]
Common Mistakes:
  • Assuming pop causes error without key check
  • Thinking list copy needed for dict modification
  • Believing function must create new list always
5. You want to train a machine learning model on health data but must ensure privacy. Which combined approach best protects privacy while allowing model training?
hard
A. Publish all data publicly but only share model predictions
B. Use raw data with no changes and share model openly
C. Encrypt data but train model without removing identifiers
D. Remove personal identifiers and apply differential privacy during training

Solution

  1. Step 1: Identify privacy protection methods

    Removing personal identifiers hides direct personal info; differential privacy adds noise to protect indirect info.
  2. Step 2: Evaluate options for privacy and training

    Using raw data or publishing data openly risks privacy; encryption alone doesn't anonymize data for training.
  3. Final Answer:

    Remove personal identifiers and apply differential privacy during training -> Option D
  4. Quick Check:

    Combine anonymization + differential privacy = Remove personal identifiers and apply differential privacy during training [OK]
Hint: Combine data cleaning with privacy techniques for safety [OK]
Common Mistakes:
  • Using raw data without protection
  • Relying only on encryption without anonymization
  • Publishing sensitive data openly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard