Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Scan and count characters where `ch >= 'A' && ch <= 'Z'`.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you write a clean range-membership tally.
Where candidates slip
Range slip including lowercase or symbols.
How to stand out
Note it generalises to count-upper-lower with a second counter.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Range count
1Hint 1Explore hintHide hint
What ASCII range holds the capital letters?
2Hint 2Explore hintHide hint
One counter, bump on a range match.
3Hint 3Explore hintHide hint
Ignore lowercase, digits, and symbols.
A · Core idea
Count characters in the uppercase ASCII range 'A'..'Z' with a single counter — the simplest range-membership tally.
Why a range check. ch >= 'A' && ch <= 'Z' is an O(1) test that includes only the 26 capital letters; lowercase letters, digits, spaces, and symbols all fail it and are ignored.
Why one pass, no early exit. A total count requires visiting every character. There's no shortcut without preprocessing.
It's the count-upper-lower problem reduced to just the uppercase bucket.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "Hi World"H
A-Z
count=12C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(1) space — one range comparison per character.
E · Edge cases and pitfalls5s = "abc" — None → 0.Explore edge casesHide edge cases
Edge cases handled
s = "abc"None → 0.s = "ABC"All upper → 3.s = ""Empty → 0.
Pitfalls
- Including lowercase letters by mistake.
- Counting non-letters.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int countUpper(String s) { int c = 0; for (char ch : s.toCharArray()) { if (ch >= 'A' && ch <= 'Z') c++; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Including lowercase letters by mistake.
- Counting non-letters.
Revise
Carry the pattern into the interview
Pattern to remember
ASCII-range count
Code pattern to memorize
c=0; for ch: if 'A'<=ch<='Z': c++Rules to remember
- 1Uppercase: A-Z.
- 2One counter.
- 3Skip everything else.
Use this pattern when
- Range-membership counting
Interview takeaway
Range-membership counting is one comparison per character.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: ASCII-range count. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups