Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Two counters; for each char test the 'A'..'Z' and 'a'..'z' ranges and increment accordingly. Non-letters are skipped.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you classify by range and exclude non-letters.
Where candidates slip
Including non-letters in a count.
How to stand out
Note the shared classify-and-count pattern across these problems.
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 classify
1Hint 1Explore hintHide hint
Two ASCII ranges separate the cases — which are they?
2Hint 2Explore hintHide hint
One counter per case; skip non-letters.
3Hint 3Explore hintHide hint
Return the counts in the agreed order.
A · Core idea
Classify each character by ASCII range — 'A'..'Z' for uppercase, 'a'..'z' for lowercase — and tally two counters. Non-letters fall in neither range and are skipped.
Why ranges, not isUpper/isLower only. The explicit range checks are O(1) and language-agnostic; Character.isUpperCase works too. Either way, the key is to exclude digits, spaces, and punctuation from both counts.
Why two counters. Uppercase and lowercase are disjoint categories; one counter each, one pass. Return them in the agreed order ([upper, lower]).
This is the same classify-and-count shape as vowels/consonants and positive/negative.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "Hi!"H
A-Z
upper=1[1, 1]C · 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 or two range checks and an increment per character.
E · Edge cases and pitfalls5s = "ABC" — → [3, 0].Explore edge casesHide edge cases
Edge cases handled
s = "ABC"→ [3, 0].s = "abc123"Digits ignored → [0, 3].s = ""→ [0, 0].
Pitfalls
- Counting digits or spaces as letters.
- Returning the counts in the wrong order.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int[] countCase(String s) { int upper = 0, lower = 0; for (char ch : s.toCharArray()) { if (ch >= 'A' && ch <= 'Z') upper++; else if (ch >= 'a' && ch <= 'z') lower++;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Counting digits or spaces as letters.
- Returning counts in the wrong order.
Revise
Carry the pattern into the interview
Pattern to remember
ASCII-range classify
Code pattern to memorize
if A<=ch<=Z upper++ elif a<=ch<=z lower++Rules to remember
- 1Upper: A-Z.
- 2Lower: a-z.
- 3Skip non-letters.
Use this pattern when
- Bucketing characters by case/category
Interview takeaway
Two ASCII ranges separate uppercase from lowercase.
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 classify. Master it on small inputs and it transfers to harder problems.