Reported September 2026
Infosysbreadth first search

Count Reachable Values by Halving and Decrementing

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

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

Infosys reported this one in September 2026, and the title hides what it really is. It looks like a counting puzzle, but it reduces to a bounded BFS over values, where each value branches into at most two children. With num up to 10^9 and steps capped at 60, the question is whether you can just walk the graph or need something smarter. If you're taking the OA soon, this is one you want a plan for before you open it. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the idea below is short enough to hold in your head.

The problem

Start from the nonnegative integer num. From any current value you may perform either allowed operation:
If the value is even, replace it with half of itself.
If the value is positive, replace it with one less.
For this exercise, assume you may perform at most steps operations. Count the distinct nonnegative values that can appear across all valid operation sequences, including the initial value reached with zero operations.

Function
countReachableValues(num: int, steps: int) → int

Examples
Example 1
num = 6
steps = 2
return = 5
The reachable values are 6, 5, 3, 4, and 2.
Example 2
num = 0
steps = 60
return = 1
Halving zero returns zero and decrement is unavailable, so only zero is reachable.
Example 3
num = 5
steps = 0
return = 1
With no operation allowed, only the initial value is counted.

Constraints
0 <= num <= 10^9.
0 <= steps <= 60.
The answer fits in a signed 32-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is noticing that steps is tiny. Each value has at most two moves, so the naive tree has up to 2^60 paths, but the set of distinct values is much smaller. Run BFS level by level with a visited set, and stop after steps levels. Halving and decrementing keep values within [0, num], and many paths collapse onto the same number, so dedup is what saves you. The pitfall is DFS without memoization, which blows up exponentially. Another one is forgetting the even check before halving, or letting zero decrement into -1. Zero halves to zero, so skip it as already visited. Also count the starting value at zero operations. Check example 1: 6 gives 5 and 3, then 4, 2 from those, so 5 values in total. The answer is the size of the visited set. If the set grows large, the guarantee that it fits in 32 bits keeps memory sane.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Count Reachable Values by Halving and Decrementing 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Infosys reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Reachable Values by Halving and Decrementing FAQ

How hard is this Infosys OA question really?+

Easy to medium. The logic is a plain BFS with a visited set. The difficulty is spotting that deduplication kills the exponential blowup. If you've written BFS on a number graph before, you can finish it in a few minutes.

What's the trick to solving it?+

Treat each value as a node with up to two edges: half if even, minus one if positive. Run BFS for exactly steps levels, track visited values in a set, and return the set size. Include the starting value.

Why not just recurse over all operation sequences?+

With steps up to 60, you'd explore up to 2^60 sequences. Many sequences land on the same value, so memoizing or using a visited set per level collapses the work to the number of distinct values, which is small.

What edge cases should I test?+

Test num = 0 with large steps, which returns 1. Test steps = 0, which returns 1. Test num = 1 and odd numbers where only decrement applies. Make sure zero never decrements to a negative value.

How do I prepare for this in 48 hours?+

Write BFS over implicit graphs twice with a visited set, such as minimum operations to reach a target number. Practice level-limited BFS where you stop after k levels. Then trace the three examples by hand so your counting matches.

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

OA at Infosys?
Invisible during screen share
Get it