Count Numbers
Reported by candidates from JP Morgan's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
JP Morgan asked this in September 2024, and it's a counting problem that looks simpler than it is. You'll see a range or constraint and need to count integers that satisfy some property. The trap is assuming brute force works when the range is huge. If you blank on optimization mid-OA, StealthCoder will spot the mathematical shortcut you're missing and feed you the pattern in real time. That's what you're paying for here.
Pattern and pitfall
Count Numbers problems at JP Morgan typically hide a digit-DP or mathematical formula underneath. The naive approach iterates through every number in a range and checks a condition, which times out. The real solution recognizes that counting with constraints (divisibility, digit patterns, specific sums) often reduces to math or dynamic programming on digit positions. Common pitfall: spending 10 minutes coding a loop that'll never pass large test cases. StealthCoder reads the actual constraints and nudges you toward the compressed solution before you're locked into the wrong approach.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Count Numbers 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass JP Morgan's OA.
JP Morgan reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Numbers FAQ
Is this a digit-DP problem?+
Likely. If the problem asks you to count integers in a range with a digit or arithmetic constraint, digit-DP (dynamic programming on digit positions) is the pattern. It avoids iterating every number and instead builds the count mathematically by position.
What's the biggest mistake candidates make?+
Writing a for loop from start to end and checking each number. It works on small examples but fails on large ranges. Recognize the constraint early. If the range is billions or the check is expensive, stop coding the loop and pivot to math or DP.
How much time should I spend on brute force before switching?+
Code it in 2-3 minutes as a proof of concept. Test on the example. Then think: does this scale? If the range is 10^9 or higher, it doesn't. Shift to mathematical or DP approach immediately.
Can I solve this in Python or do I need C++?+
Python is fine if your algorithm is right. The bottleneck is always algorithmic, not language. A correct O(log n) solution in Python beats a wrong O(n) solution in C++ every time.
What if I can't figure out the pattern in 10 minutes?+
Write the brute-force solution, pass what you can, and explain your thinking. JP Morgan values the approach. But that's when StealthCoder steps in and shows you the optimization so you're not guessing on the harder test cases.