Bird
Raised Fist0
NLPml~20 mins

Jaccard similarity 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
🎖️
Jaccard Similarity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Jaccard similarity calculation?
Given two sets A = {1, 2, 3, 4} and B = {3, 4, 5, 6}, what is the Jaccard similarity computed by the code below?
NLP
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
intersection = A.intersection(B)
union = A.union(B)
jaccard_similarity = len(intersection) / len(union)
print(round(jaccard_similarity, 2))
A0.50
B0.33
C0.25
D0.40
Attempts:
2 left
💡 Hint
Recall that Jaccard similarity is the size of the intersection divided by the size of the union of two sets.
🧠 Conceptual
intermediate
1:30remaining
Which statement best describes Jaccard similarity?
Choose the best description of what Jaccard similarity measures between two sets.
AThe ratio of the size of the intersection to the size of the union of two sets.
BThe difference in sizes between two sets.
CThe sum of the sizes of two sets.
DThe ratio of the size of the union to the size of the intersection of two sets.
Attempts:
2 left
💡 Hint
Think about how much two sets overlap compared to their total combined size.
Metrics
advanced
2:00remaining
What is the Jaccard similarity between these two token sets?
Given two token sets from text documents: doc1 = {'apple', 'banana', 'cherry'} and doc2 = {'banana', 'cherry', 'date', 'fig'}, what is their Jaccard similarity?
A0.4
B0.5
C0.6
D0.75
Attempts:
2 left
💡 Hint
Count the common tokens and total unique tokens.
🔧 Debug
advanced
2:00remaining
Why does this Jaccard similarity code raise an error?
Consider this code snippet:
def jaccard_similarity(list1, list2):
    intersection = list1 & list2
    union = list1 | list2
    return len(intersection) / len(union)

print(jaccard_similarity(['a', 'b'], ['b', 'c']))
Why does it raise an error?
AThe function returns a float but print expects a string.
BThe function is missing a return statement.
CLists do not support the '&' and '|' operators; sets are needed.
DThe lists are empty, causing division by zero.
Attempts:
2 left
💡 Hint
Check the data types and operators used for intersection and union.
Model Choice
expert
3:00remaining
Which model output best matches Jaccard similarity for text similarity?
You have two text documents and want to measure their similarity using Jaccard similarity on token sets. Which model output below correctly computes this similarity?
A
def jaccard(doc1, doc2):
    return len(set(doc1).intersection(set(doc2))) / len(set(doc1).union(set(doc2)))
B
def jaccard(doc1, doc2):
    tokens1, tokens2 = doc1.split(), doc2.split()
    return len(tokens1 & tokens2) / len(tokens1 | tokens2)
C
def jaccard(doc1, doc2):
    set1, set2 = set(doc1), set(doc2)
    return len(set1 & set2) / len(set1 | set2)
D
def jaccard(doc1, doc2):
    set1, set2 = set(doc1.split()), set(doc2.split())
    return len(set1.intersection(set2)) / len(set1.union(set2))
Attempts:
2 left
💡 Hint
Consider how to tokenize text properly before computing sets.

Practice

(1/5)
1. What does the Jaccard similarity measure between two sets?
easy
A. The difference between the sizes of the two sets
B. The size of the union divided by the size of the intersection
C. The sum of the sizes of the two sets
D. The size of the intersection divided by the size of the union

Solution

  1. Step 1: Understand the definition of Jaccard similarity

    Jaccard similarity is defined as the size of the intersection of two sets divided by the size of their union.
  2. Step 2: Compare options with the definition

    The size of the intersection divided by the size of the union matches the definition exactly, while others describe different calculations.
  3. Final Answer:

    The size of the intersection divided by the size of the union -> Option D
  4. Quick Check:

    Jaccard similarity = intersection / union [OK]
Hint: Remember: overlap divided by total unique items [OK]
Common Mistakes:
  • Confusing union with intersection
  • Using subtraction instead of division
  • Mixing up numerator and denominator
2. Which of the following Python code snippets correctly calculates the Jaccard similarity between two sets A and B?
easy
A. len(A | B) / len(A & B)
B. len(A & B) / len(A | B)
C. len(A - B) / len(B - A)
D. len(A) + len(B)

Solution

  1. Step 1: Identify set operations for intersection and union

    In Python, & is intersection and | is union for sets.
  2. Step 2: Check the formula for Jaccard similarity

    Jaccard similarity = size of intersection / size of union, which matches len(A & B) / len(A | B).
  3. Final Answer:

    len(A & B) / len(A | B) -> Option B
  4. Quick Check:

    Intersection & union operators used correctly [OK]
Hint: Use & for intersection, | for union in Python sets [OK]
Common Mistakes:
  • Swapping intersection and union operators
  • Using subtraction instead of intersection
  • Adding lengths instead of dividing
3. Given two sets A = {'apple', 'banana', 'cherry'} and B = {'banana', 'cherry', 'date', 'fig'}, what is the Jaccard similarity computed by this code?
len(A & B) / len(A | B)
medium
A. 0.4
B. 0.5
C. 0.6
D. 0.75

Solution

  1. Step 1: Calculate intersection and union of sets A and B

    Intersection: {'banana', 'cherry'} has 2 elements.
    Union: {'apple', 'banana', 'cherry', 'date', 'fig'} has 5 elements.
  2. Step 2: Compute Jaccard similarity

    Similarity = 2 / 5 = 0.4.
  3. Final Answer:

    0.4 -> Option A
  4. Quick Check:

    2 / 5 = 0.4 [OK]
Hint: Count common and total unique items, then divide [OK]
Common Mistakes:
  • Counting union incorrectly
  • Using addition instead of division
  • Mixing up intersection and union counts
4. The following code is intended to compute the Jaccard similarity between two sets A and B. What is the error?
def jaccard(A, B):
    return len(A & B) / len(A & B | B)
medium
A. Function missing return statement
B. Division by zero error possible
C. Incorrect use of union and intersection operators in denominator
D. Sets A and B are not defined

Solution

  1. Step 1: Analyze the denominator expression

    The denominator is len(A & B | B). The operator precedence causes A & B to be evaluated first, then union with B. This results in len(B), which is incorrect for union of A and B.
  2. Step 2: Correct denominator for union

    The union should be len(A | B) only. The current expression is wrong and will not compute union correctly.
  3. Final Answer:

    Incorrect use of union and intersection operators in denominator -> Option C
  4. Quick Check:

    Union must be A | B, not combined with & [OK]
Hint: Use parentheses or correct operators for union [OK]
Common Mistakes:
  • Confusing operator precedence
  • Using intersection inside union calculation
  • Not testing code before use
5. You want to compare two documents by their unique words using Jaccard similarity. Document 1 has 100 unique words, Document 2 has 80 unique words, and they share 30 unique words. What is the Jaccard similarity? Also, if you add 20 common words to both documents, how does the similarity change?
hard
A. Initial similarity 0.2; after adding common words similarity increases to 0.3
B. Initial similarity 0.15; after adding common words similarity decreases
C. Initial similarity 0.25; after adding common words similarity stays the same
D. Initial similarity 0.18; after adding common words similarity increases to 0.33

Solution

  1. Step 1: Calculate initial Jaccard similarity

    Intersection = 30
    Union = 100 + 80 - 30 = 150
    Similarity = 30 / 150 = 0.2
  2. Step 2: Calculate similarity after adding 20 common words

    New intersection = 30 + 20 = 50
    New union = (100 + 20) + (80 + 20) - 50 = 170
    Similarity = 50 / 170 ≈ 0.2941, approximately 0.3
  3. Final Answer:

    Initial similarity 0.2; after adding common words similarity increases to 0.3 -> Option A
  4. Quick Check:

    Adding common words increases intersection and similarity [OK]
Hint: Adding shared items increases similarity numerator and denominator [OK]
Common Mistakes:
  • Forgetting to subtract intersection in union
  • Not updating intersection after adding words
  • Assuming similarity stays constant