MEDIUMasked at 10 companies

Count Primes

A medium-tier problem at 35% community acceptance, tagged with Array, Math, Enumeration. Reported in interviews at Tech Mahindra and 9 others.

Founder's read

Count Primes is a medium-difficulty problem that shows up in assessments from Walmart Labs, Intel, Salesforce, TikTok, and others. You're given a number n and asked to count how many primes exist below it. The naive approach (check every number individually) tanks on large inputs. With a 35% acceptance rate, most candidates either time out or overthink the logic. If this hits your live OA and you blank on the sieve, StealthCoder surfaces the working solution invisibly while you stay on screen.

Companies asking
10
Difficulty
MEDIUM
Acceptance
35%

Companies that ask "Count Primes"

If this hits your live OA

Count Primes is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE.

Get StealthCoder
What this means

The trick here is the Sieve of Eratosthenes, a 2000-year-old algorithm that's shockingly efficient at filtering primes in bulk. You mark multiples of each prime as composite, not checking divisibility for every single candidate. The naive approach checks each number up to n for divisibility by all primes below it; the sieve does it once, in O(n log log n) time. Most failures happen because candidates implement the sieve incorrectly (off-by-one bugs, wrong initialization) or don't realize the naive approach will timeout. This is a Number Theory problem dressed as Array work. StealthCoder is the hedge if the algorithm doesn't click during the real assessment.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Count Primes recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Primes interview FAQ

Why does the naive divisibility check fail here?+

You'd check every number up to n against all primes below its square root. On n = 10^6, that's hundreds of millions of operations. The sieve does it once: mark all multiples of 2, then 3, then 5, and so on. Linear sweep beats repeated division every time.

Is this still asked at big tech companies?+

Yes. Walmart Labs, Intel, Salesforce, and TikTok all have it in their assessments. It's a classic filter problem. Lower frequency at ultra-large FAANG, but it appears regularly at established enterprise and mid-tier tech shops.

What's the biggest trap in implementing the sieve?+

Off-by-one on array bounds and marking the starting point of multiples. Mark composites starting at i*i, not 2*i, to avoid redundant work. Also, 0 and 1 are not prime by definition, so initialize your boolean array carefully.

Do I need to know this pattern cold before my OA?+

Ideally yes, because it's straightforward once you see it. But if you hit it blank and you're on screen share, you can't afford to derive it live. That's why you drill it beforehand or have a safety net ready.

How does this relate to the other Number Theory topics tested?+

Primes are foundational in number theory. Once you sieve them efficiently, you can solve GCD, modular arithmetic, and factorization problems faster. This is a gateway pattern.

Want the actual problem statement? View "Count Primes" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.