Largest Group of Account IDs Linked by a Shared Prime Factor

Last updated: November 29, 2026

Quick Overview

Given a list of distinct positive integers, connect two numbers if they share a common factor greater than 1. Return the size of the largest connected group.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given a list of distinct positive integers, connect two numbers if they share a common factor greater than 1. Return the size of the largest connected group.

A hard Meta onsite question that tests whether you can avoid an O(n^2) gcd comparison by routing unions through shared prime factors. The interviewer is checking factorization efficiency, a union find keyed on values, and clear complexity reasoning.

What the Interviewer Expects
  • Reject pairwise gcd as O(n^2 log V)
  • Factor each number into distinct primes
  • Union each number with its prime factors as intermediate nodes
  • Count group sizes over the original numbers only
  • Bound factorization at O(sqrt(V)) per number or use a sieve
Key Topics to Cover
union find
prime factorization
number theory
connected components
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
  • How would a smallest-prime-factor sieve change the complexity?
  • What if numbers can repeat, or include 1?
  • How would you return the members of the largest group?
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
  • Input: nums, distinct positive integers up to about 10^5.
  • Edge between a and b when gcd(a, b) > 1.
  • Output: size of the largest component....
High-Level Design

Pairwise gcd checks are O(n^2). Instead notice that a and b share a factor exactly when they share a prime. So treat each prime as a hub node: union every number with each of its prime factors. ...


Submit Your Answer
Markdown supported