Group images into similarity clusters using edges that meet a score threshold

Quick Overview

Given images linked by weighted similarity edges, group them into clusters where every edge on a connecting path meets a score threshold, then return the clusters in sorted order. It tests connected components, Union-Find with path compression, and producing a deterministic output order.

Group images into similarity clusters using edges that meet a score threshold

Company: Pinterest

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given `n` images labeled from `0` to `n - 1` and an array `edges` where `edges[i] = [u, v, score]` represents an undirected edge meaning images `u` and `v` have a similarity score of `score`. You are also given an integer `threshold`. Two images belong to the same cluster if they are connected by a path where every edge on the path has a similarity score of at least `threshold`. Every image belongs to exactly one cluster; an image with no qualifying edges forms a cluster by itself. Return all clusters. Each cluster should list its image labels in ascending order, and the clusters should be sorted by their first element. ### Function Signature ```python def find_similar_image_clusters(n: int, edges: list[list[int]], threshold: int) -> list[list[int]]: ``` ### Rules - An edge qualifies when `score >= threshold`. An edge with a score equal to the threshold connects its two images. - Return a list of clusters, each a list of image labels. Every label from `0` to `n - 1` appears in exactly one cluster. - Labels inside a cluster are sorted ascending. Clusters are sorted by their first (smallest) label. Clusters never share a label, so this order is unique. ### Constraints - `1 <= n <= 10^4` - `0 <= edges.length <= 10^5` - `edges[i].length == 3` - `0 <= u, v < n` - `u != v` - `0 <= score <= 100` - `0 <= threshold <= 100` ### Examples ```text Input: n = 5, edges = [[0,1,80],[1,2,90],[3,4,40],[2,3,30]], threshold = 50 Output: [[0,1,2],[3],[4]] ``` The images form a chain 0, 1, 2, 3, 4 with scores 80, 90, 30 and 40. Only edges 0-1 (80) and 1-2 (90) meet the threshold of 50, so images 0, 1 and 2 form a cluster. Images 3 and 4 are each on their own. ```text Input: n = 4, edges = [[0,1,70],[2,3,70],[1,2,60]], threshold = 60 Output: [[0,1,2,3]] ``` All edges have scores of at least 60 (the edge 1-2 is exactly 60), so all four images form one cluster.

Overview: Given images linked by weighted similarity edges, group them into clusters where every edge on a connecting path meets a score threshold, then return the clusters in sorted order. It tests connected components, Union-Find with path compression, and producing a deterministic output order.

Pinterest logo
Pinterest
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given n images labeled from 0 to n - 1 and an array edges where edges[i] = [u, v, score] represents an undirected edge meaning images u and v have a similarity score of score. You are also given an integer threshold.

Two images belong to the same cluster if they are connected by a path where every edge on the path has a similarity score of at least threshold. Every image belongs to exactly one cluster; an image with no qualifying edges forms a cluster by itself.

Return all clusters. Each cluster should list its image labels in ascending order, and the clusters should be sorted by their first element.

Function Signature

def find_similar_image_clusters(n: int, edges: list[list[int]], threshold: int) -> list[list[int]]:

Rules

  • An edge qualifies when score >= threshold . An edge with a score equal to the threshold connects its two images.
  • Return a list of clusters, each a list of image labels. Every label from 0 to n - 1 appears in exactly one cluster.
  • Labels inside a cluster are sorted ascending. Clusters are sorted by their first (smallest) label. Clusters never share a label, so this order is unique.

Constraints

  • 1 <= n <= 10^4
  • 0 <= edges.length <= 10^5
  • edges[i].length == 3
  • 0 <= u, v < n
  • u != v
  • 0 <= score <= 100
  • 0 <= threshold <= 100

Examples

Input: n = 5, edges = [[0,1,80],[1,2,90],[3,4,40],[2,3,30]], threshold = 50
Output: [[0,1,2],[3],[4]]

The images form a chain 0, 1, 2, 3, 4 with scores 80, 90, 30 and 40. Only edges 0-1 (80) and 1-2 (90) meet the threshold of 50, so images 0, 1 and 2 form a cluster. Images 3 and 4 are each on their own.

Input: n = 4, edges = [[0,1,70],[2,3,70],[1,2,60]], threshold = 60
Output: [[0,1,2,3]]

All edges have scores of at least 60 (the edge 1-2 is exactly 60), so all four images form one cluster.