Reported September 2026
Accenturemath

Nearest Composite Number

Reported by candidates from Accenture's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Accenture OA. Under 2s to a working solution.
Founder's read

The Accenture OA reported in September 2026 has a problem that looks like a freebie and then bites on small inputs: Nearest Composite Number. Given n, return the composite with the smallest absolute difference, and pick the smaller one on a tie. It's a math and primality check problem, nothing exotic. The trap is n values like 0, 1, 2 and 3, where the nearest composite sits above n. If you've got an invite for this one, the edge cases are the whole game. StealthCoder runs invisibly as a safety net if you blank during the live OA.

The problem

Return the composite integer with the smallest absolute difference from n. A composite integer is greater than 1 and has a divisor other than 1 and itself.
If two composite numbers are equally near, return the smaller one.

Function
nearestComposite(n: int) → int

Examples
Example 1
n = 4
return = 4
Case 1 exercises the documented deterministic contract.
Example 2
n = 5
return = 4
Case 2 exercises the documented deterministic contract.
Example 3
n = 7
return = 6
Case 3 exercises the documented deterministic contract.

Constraints
0 <= n <= 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: composites are dense. If n itself is composite (n >= 4 and not prime), return n. Otherwise search outward: check n-1, n+1, n-2, n+2 and so on, always testing the lower candidate first so ties resolve to the smaller number. Primes are sparse, so you'll hit a composite within a step or two. Use trial division up to sqrt(x), which is fast enough for 10^9. The pitfall is the low end. Composites start at 4, so for n = 0, 1, 2, 3 the lower candidate is invalid (below 4) and the answer is 4. Don't treat 0 or 1 as composite, and don't let the lower search go negative. A clean guard: if n <= 4 return 4. If you freeze on the tie rule or the boundary logic mid-assessment, StealthCoder is the hedge that hands you the working loop while the proctor sees nothing.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Nearest Composite Number cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Accenture's OA.

Accenture reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Nearest Composite Number FAQ

What's the trick in Nearest Composite Number?+

Composites are everywhere, so the answer is never far from n. Return n if it's composite, otherwise step outward checking n-d before n+d. Checking the lower side first handles the tie rule for free. The real work is the small-n edge cases.

What inputs break a naive solution?+

n = 0, 1, 2 and 3. Composites start at 4, so the answer for all of those is 4. Also watch n = 5 (returns 4) and n = 7 (returns 6). A solution that treats 1 as composite or searches below 4 will fail.

How do I check primality for n up to 10^9?+

Trial division up to sqrt(x), about 31623 iterations at most. You only test a handful of candidates since primes are sparse, so total cost is tiny. No sieve needed, and a sieve over 10^9 would blow memory.

How do I handle ties between two composites?+

Ties go to the smaller number. In your outward loop, test n-d before n+d for each distance d. The first composite found wins, and if both are composite at the same d, the lower one is returned first.

How should I prepare for this in 48 hours?+

Write the solution once from scratch and test it against n = 0, 1, 2, 3, 4, 5, 7, and a large prime near 10^9. Confirm the primality helper returns false for 0 and 1. That covers nearly every failure mode this problem has.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Accenture.

OA at Accenture?
Invisible during screen share
Get it