Nth Valid Shard Size Built Only From Allowed Prime Factors
Last updated: November 29, 2026
Quick Overview
A shard size is valid if its only prime factors come from a given sorted list of primes (1 counts as valid). Return the nth smallest valid shard size.
Meta
November 29, 20260
0
0 solved
A shard size is valid if its only prime factors come from a given sorted list of primes (1 counts as valid). Return the nth smallest valid shard size.
A Meta technical-screen variant of the generalized ugly-number problem, framed around storage shard sizing. It tests whether you can generate an ordered sequence lazily with a heap and avoid duplicates such as 6 = 2 * 3 = 3 * 2.
What the Interviewer Expects
- Generate values in sorted order rather than testing each integer
- Keep one pointer per prime into the generated list
- Use a heap to find the next smallest candidate
- Deduplicate equal candidates from different primes
- Analyze O(n log p)
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Do it without a heap in O(n * p).
- What if n is 10^9?
- What if the prime list is very long (thousands)?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
n >= 1, list of distinct primes. - Output: the nth value in the ascending sequence of numbers whose prime factors are all in the list, starting with 1....
High-Level Design
Every valid number after 1 is some earlier valid number times one allowed prime. So the sequence is a merge of p sorted streams: sizes[i] * prime_j for each j.
Keep a pointer idx[j] per prime int...