Bird
Raised Fist0
NLPml~20 mins

Bias and fairness in NLP - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Bias and Fairness Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Bias Types in NLP

Which of the following best describes representation bias in natural language processing?

ABias caused by errors in tokenization or text preprocessing steps.
BBias introduced by the model architecture that favors certain words over others.
CBias caused by unbalanced or skewed training data that underrepresents certain groups or topics.
DBias that occurs only during model deployment due to hardware limitations.
Attempts:
2 left
💡 Hint

Think about how the data itself might not fairly represent all groups or topics.

Metrics
intermediate
2:00remaining
Measuring Fairness with Equality of Opportunity

In a binary classification NLP task, which metric best captures equality of opportunity between two demographic groups?

ADifference in true positive rates (TPR) between the groups.
BDifference in overall accuracy between the groups.
CDifference in false positive rates (FPR) between the groups.
DDifference in precision between the groups.
Attempts:
2 left
💡 Hint

Equality of opportunity focuses on equal chances to correctly identify positive cases.

Predict Output
advanced
3:00remaining
Output of Bias Mitigation Code Snippet

What is the output of the following Python code that applies simple bias mitigation by equalizing sample counts?

NLP
from collections import Counter

data = [('male', 'positive'), ('female', 'positive'), ('male', 'negative'), ('male', 'positive'), ('female', 'negative')]

# Count samples per gender
counts = Counter([d[0] for d in data])
min_count = min(counts.values())

# Equalize samples by truncating
balanced_data = []
counts_seen = {'male': 0, 'female': 0}
for d in data:
    gender = d[0]
    if counts_seen[gender] < min_count:
        balanced_data.append(d)
        counts_seen[gender] += 1

print(balanced_data)
A[('male', 'positive'), ('female', 'positive'), ('male', 'negative')]
B[('female', 'positive'), ('female', 'negative')]
C[('male', 'positive'), ('male', 'negative')]
D[('male', 'positive'), ('female', 'positive'), ('male', 'negative'), ('female', 'negative')]
Attempts:
2 left
💡 Hint

Check how many samples each gender has and how many are kept after truncation.

Model Choice
advanced
2:30remaining
Choosing a Model Architecture to Reduce Gender Bias

Which model architecture is best suited to reduce gender bias in a sentiment analysis task on social media text?

AA simple logistic regression model trained on raw word counts.
BA transformer model with adversarial training to remove gender information from embeddings.
CA convolutional neural network without any bias mitigation techniques.
DA recurrent neural network trained only on male-authored texts.
Attempts:
2 left
💡 Hint

Consider architectures that explicitly try to remove bias signals.

🔧 Debug
expert
3:00remaining
Debugging Fairness Metric Calculation

Given this code snippet to compute demographic parity difference, what error or issue will it cause?

NLP
def demographic_parity_difference(preds, groups):
    # preds: list of 0/1 predictions
    # groups: list of group labels (e.g., 'A', 'B')
    group_pos_rates = {}
    for g in set(groups):
        group_preds = [p for p, grp in zip(preds, groups) if grp == g]
        group_pos_rates[g] = sum(group_preds) / len(group_preds)
    return abs(group_pos_rates['A'] - group_pos_rates['B'])

# Example usage
preds = [1, 0, 1, 1, 0]
groups = ['A', 'A', 'B', 'B', 'B']
print(demographic_parity_difference(preds, groups))
ANo error; outputs 0.16666666666666663.
BZeroDivisionError if any group has zero members.
CKeyError if groups contain labels other than 'A' or 'B'.
DTypeError because sum is used on non-numeric data.
Attempts:
2 left
💡 Hint

Check the inputs and how the function handles group labels.

Practice

(1/5)
1. What does bias in NLP models usually mean?
easy
A. The model always predicts correctly
B. Unfair treatment of some groups by the model
C. The model runs faster on some data
D. The model uses more memory for some inputs

Solution

  1. Step 1: Understand the meaning of bias in NLP

    Bias refers to when a model treats some groups unfairly, often due to skewed training data or design.
  2. Step 2: Compare options to definition

    Only Unfair treatment of some groups by the model describes unfair treatment, which matches the definition of bias in NLP.
  3. Final Answer:

    Unfair treatment of some groups by the model -> Option B
  4. Quick Check:

    Bias = Unfair treatment [OK]
Hint: Bias means unfairness in model predictions [OK]
Common Mistakes:
  • Confusing bias with model speed or memory use
  • Thinking bias means always correct predictions
2. Which of the following is the correct way to check fairness in an NLP model?
easy
A. Count the number of layers in the model
B. Check if the model uses GPU acceleration
C. Compare accuracy across different demographic groups
D. Measure the model's training time

Solution

  1. Step 1: Identify fairness checking methods

    Fairness is checked by comparing performance metrics like accuracy across groups to ensure equal treatment.
  2. Step 2: Evaluate options

    Only Compare accuracy across different demographic groups relates to fairness by comparing accuracy across groups; others are unrelated to fairness.
  3. Final Answer:

    Compare accuracy across different demographic groups -> Option C
  4. Quick Check:

    Fairness check = Compare accuracy by group [OK]
Hint: Fairness means equal accuracy for all groups [OK]
Common Mistakes:
  • Confusing fairness with model speed or architecture
  • Ignoring group-based performance differences
3. Consider this Python code snippet checking fairness metrics:
group_accuracies = {'groupA': 0.85, 'groupB': 0.60}
if abs(group_accuracies['groupA'] - group_accuracies['groupB']) > 0.2:
    print('Fairness issue detected')
else:
    print('No fairness issue')
What will this code print?
medium
A. KeyError
B. No fairness issue
C. SyntaxError
D. Fairness issue detected

Solution

  1. Step 1: Calculate difference in accuracies

    The difference is |0.85 - 0.60| = 0.25, which is greater than 0.2.
  2. Step 2: Evaluate the if condition

    Since 0.25 > 0.2, the condition is true, so it prints 'Fairness issue detected'.
  3. Final Answer:

    Fairness issue detected -> Option D
  4. Quick Check:

    Difference 0.25 > 0.2 = Fairness issue [OK]
Hint: Check if accuracy difference > threshold for fairness [OK]
Common Mistakes:
  • Miscomputing the absolute difference
  • Confusing greater than with less than
  • Expecting syntax or key errors
4. This code tries to calculate fairness but has a bug:
metrics = {'group1': {'accuracy': 0.9}, 'group2': {'accuracy': 0.85}}
diff = metrics['group1']['accuracy'] - metrics['group3']['accuracy']
if abs(diff) > 0.05:
    print('Bias detected')
What is the error and how to fix it?
medium
A. KeyError because 'group3' does not exist; fix by checking keys first
B. SyntaxError due to missing colon; fix by adding colon
C. TypeError because accuracy is not a number; fix by converting to float
D. No error; code runs fine

Solution

  1. Step 1: Identify the error cause

    The code accesses metrics['group3'], which is not in the dictionary, causing a KeyError.
  2. Step 2: Suggest fix

    Check if 'group3' exists in metrics before accessing or handle missing keys to avoid error.
  3. Final Answer:

    KeyError because 'group3' does not exist; fix by checking keys first -> Option A
  4. Quick Check:

    Missing key access = KeyError [OK]
Hint: Check dictionary keys before access to avoid KeyError [OK]
Common Mistakes:
  • Assuming all keys exist without checking
  • Confusing KeyError with SyntaxError or TypeError
5. You have an NLP sentiment model that predicts positive or negative sentiment. You notice it predicts positive sentiment 90% for group A but only 60% for group B, though both groups have similar real sentiment. What is the best way to improve fairness?
hard
A. Collect more balanced training data including both groups equally
B. Increase model size to improve overall accuracy
C. Use a faster optimizer to train the model
D. Remove group B data from training to avoid confusion

Solution

  1. Step 1: Understand the fairness problem

    The model predicts differently for groups with similar real sentiment, indicating bias likely from unbalanced data.
  2. Step 2: Choose the best fix

    Collecting balanced data ensures the model learns equally from both groups, improving fairness.
  3. Final Answer:

    Collect more balanced training data including both groups equally -> Option A
  4. Quick Check:

    Balanced data improves fairness [OK]
Hint: Balanced data helps fix bias in predictions [OK]
Common Mistakes:
  • Thinking bigger models fix bias automatically
  • Ignoring data imbalance as cause of unfairness
  • Removing data from minority groups