PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates understanding of graph connectivity and equivalence relations, requiring recognition of connected components from an adjacency-matrix representation of pairwise similarity.

  • medium
  • Bytedance
  • Coding & Algorithms
  • Software Engineer

Count Similar Photo Groups

Company: Bytedance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given `n` photos labeled `0` to `n - 1` and an `n x n` binary matrix `isSimilar`. - `isSimilar[i][j] = 1` means photo `i` and photo `j` are **directly similar**. - `isSimilar[i][j] = 0` means they are not directly similar. Similarity is also **transitive**: if photo `A` is similar to `B`, and `B` is similar to `C`, then `A` is considered similar to `C` even if `A` and `C` are not directly similar. Treat each set of directly or indirectly similar photos as one **photo group**. Return the total number of photo groups. You may assume the similarity relationship is symmetric, so if `isSimilar[i][j] = 1`, then `isSimilar[j][i] = 1`.

Quick Answer: This question evaluates understanding of graph connectivity and equivalence relations, requiring recognition of connected components from an adjacency-matrix representation of pairwise similarity.

You are given a binary matrix `isSimilar` representing similarity relationships among photos. Photo indices range from `0` to `n - 1`, where `n` is the number of photos. - `isSimilar[i][j] = 1` means photo `i` and photo `j` are directly similar. - `isSimilar[i][j] = 0` means they are not directly similar. Similarity is transitive: if photo `A` is similar to `B`, and `B` is similar to `C`, then `A` and `C` belong to the same group even if `isSimilar[A][C] = 0`. Treat each set of directly or indirectly similar photos as one photo group. Return the total number of photo groups. If the input matrix is empty, return `0`.

Constraints

  • 0 <= n <= 200
  • `isSimilar` is an `n x n` matrix
  • Each value in `isSimilar` is either `0` or `1`
  • `isSimilar[i][j] == isSimilar[j][i]` for all valid `i`, `j`

Examples

Input: []

Expected Output: 0

Explanation: There are no photos, so there are no groups.

Input: [[1]]

Expected Output: 1

Explanation: A single photo forms one group by itself.

Input: [[1,1,0],[1,1,0],[0,0,1]]

Expected Output: 2

Explanation: Photos 0 and 1 are connected, while photo 2 is alone, giving 2 groups.

Input: [[1,0,0],[0,1,0],[0,0,1]]

Expected Output: 3

Explanation: No two different photos are similar, so each photo is its own group.

Input: [[1,1,0,0],[1,1,1,0],[0,1,1,0],[0,0,0,1]]

Expected Output: 2

Explanation: Photos 0, 1, and 2 are all connected through transitivity, and photo 3 is separate.

Input: [[1,0,0,1,0],[0,1,1,0,0],[0,1,1,0,0],[1,0,0,1,0],[0,0,0,0,1]]

Expected Output: 3

Explanation: The groups are {0,3}, {1,2}, and {4}, so the answer is 3.

Hints

  1. Think of each photo as a node in an undirected graph, and each `1` as an edge.
  2. Count how many times you need to start a new DFS or BFS from an unvisited photo.
Last updated: May 4, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Minimize Increments to Equalize Path Costs - Bytedance (medium)
  • Implement Sorted Search and Array Updates - Bytedance (medium)
  • Find Maximum Candies With Two Types - Bytedance (medium)
  • Implement Sliding Windows and LRU Cache - Bytedance (medium)
  • Place Non-Attacking Queens - Bytedance (hard)