Cluster Near-Duplicate Training Documents From Similarity Pairs

Last updated: November 29, 2026

Quick Overview

Given n documents and a list of (i, j, similarity) pairs from a near-duplicate detector, group documents into clusters where any chain of pairs at or above a threshold links them, and return clusters of size two or more.

Anthropic
Coding & Algorithms
Machine Learning Engineer
Anthropic
November 29, 2026
Machine Learning Engineer
Technical Screen
Coding & Algorithms
Easy

0

0

0 solved


Given n documents and a list of (i, j, similarity) pairs from a near-duplicate detector, group documents into clusters where any chain of pairs at or above a threshold links them, and return clusters of size two or more.

Deduplicating pretraining data is a real concern for model quality and memorization, and pipelines often emit candidate duplicate pairs (for example from MinHash) that must be merged transitively. The interviewer checks that you see this as connected components, implement union-find with path compression and union by size, and think about deterministic output.

What the Interviewer Expects
  • Recognizes transitive grouping as connected components
  • Implements union-find with path compression and union by size or rank
  • Filters pairs by threshold before union and ignores self-pairs
  • Returns clusters in a deterministic order with documents sorted
  • Discusses why chaining can merge dissimilar documents through intermediates
Key Topics to Cover
union find
connected components
deduplication
graph algorithms
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Chaining means A matches B and B matches C even if A and C are dissimilar. How would you limit that drift?
  • Pairs arrive as a stream of billions of rows. How would you run this on a single machine with bounded memory?
  • How would you pick which document to keep from each cluster?
  • How would you distribute union-find across many workers?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Documents are ids 0..n-1. A pair with score >= threshold means duplicates, and duplication is transitive. Return a list of clusters, each a sorted list of ids, ordered by smallest id, omitting sin...

High-Level Design

Union-find over n ids. For each qualifying pair, union the two sets. Then group every id by its root. Iterating ids in ascending order means each group is built already sorted, and sorting groups by f...


Submit Your Answer
Markdown supported