Is Happy Number
Reported by candidates from Blackrock's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Blackrock asked this in August 2024, and candidates either nail it fast or spiral. You get a number, and you need to determine if it's a happy number by repeatedly summing the squares of its digits until you either reach 1 (happy) or loop forever (not happy). The trick is recognizing the cycle. StealthCoder catches you if you blank on the detection logic during the live OA.
Pattern and pitfall
The core pattern is cycle detection using a hash set. You keep transforming the number by summing digit squares. If you hit 1, return true. If you see the same number twice, you're in an infinite loop and return false. Most candidates miss that you need to track visited states, not just count iterations. Common mistake: trying to guess when the cycle starts instead of storing every intermediate result. The pattern is simple enough that Blackrock expects clean, efficient code. Knowing to use a set for cycle detection is what separates a pass from a re-do.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Is Happy 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as happy number. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Blackrock's OA.
Blackrock reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Is Happy Number FAQ
How do I actually detect the cycle?+
Store every number you generate in a set. If you compute a number that's already in the set, you're in a loop and should return false. If you hit 1, return true. No magic needed, just tracking state.
What's the trick Blackrock is looking for?+
They want to see that you know when to use a hash set for cycle detection. It's a pattern question disguised as math. If you try brute-force iteration counts, you'll fail on edge cases. Cycle detection is the real ask.
How fast does this need to run?+
Very fast. Input numbers are usually small (under 10 to the power of 10), so even a naive hash-based solution runs instantly. Blackrock doesn't care about micro-optimizations here. Correctness and clean code matter more.
Can I solve this without a set?+
Technically yes, using Floyd's cycle detection (tortoise and hare), but that's overengineering for what Blackrock expects. A set is clear, readable, and fast enough. Don't overthink it.
What if I blank on the algorithm during the OA?+
Write out the brute-force first: loop, transform, check if 1. Then ask yourself how you'd know when to stop. The answer is cycle tracking. That's your on-the-fly insight. StealthCoder hands you the full pattern if you need it.