Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
For each char, if it's between 'a' and 'z', subtract 32 from its ASCII code; leave everything else alone. Or just call `toUpperCase()`.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you know the 32 ASCII offset and guard the letter range.
Where candidates slip
Wrong direction, or shifting non-letters.
How to stand out
Mention locale-aware toUpperCase for non-ASCII correctness.
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
ASCII shift
1Hint 1Explore hintHide hint
What's the fixed ASCII gap between 'a' and 'A'?
2Hint 2Explore hintHide hint
Subtract 32 from a–z letters; guard the range.
3Hint 3Explore hintHide hint
Leave digits, spaces, and symbols unchanged.
A · Core idea
Convert lowercase to uppercase with an ASCII shift: lowercase letters sit exactly 32 code points above their uppercase forms ('a'=97, 'A'=65), so subtracting 32 from an a–z letter yields its uppercase version.
Why the range guard. Only characters in 'a'..'z' should shift. Subtracting 32 blindly would corrupt digits, spaces, and symbols. Guard with 'a' <= ch <= 'z' and leave everything else untouched.
Direction matters. Subtract 32 to go up to uppercase; adding 32 would lowercase instead — a common sign slip.
In practice. You'd call toUpperCase(), which also handles locale and non-Latin scripts (e.g. Turkish 'i'). The manual shift is the ASCII-fundamentals demonstration; mention the locale nuance for credit.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "aB1"a
in a-z → −32
A"AB1"C · Code with teaching comments
D · Why it works and complexityO(n) timeO(n) spaceOpen the invariant and proofExplore the proofHide the proof
Knowing the 32 offset between cases is a classic ASCII fundamentals question.
ComplexityO(n) time, O(n) space — one range check and possible shift per character.
E · Edge cases and pitfalls5s = "hello" — → "HELLO".Explore edge casesHide edge cases
Edge cases handled
s = "hello"→ "HELLO".s = "aBc1"Digit unchanged → "ABC1".s = "X"Already upper → "X".
Pitfalls
- Shifting non-letters and corrupting digits/symbols.
- Adding 32 (lowercases) instead of subtracting.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String toUpper(String s) { char[] c = s.toCharArray(); for (int i = 0; i < c.length; i++) { if (c[i] >= 'a' && c[i] <= 'z') c[i] -= 32; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Shifting non-letters and corrupting digits/symbols.
- Adding 32 (that lowercases) instead of subtracting.
Revise
Carry the pattern into the interview
Pattern to remember
ASCII case shift
Code pattern to memorize
if 'a'<=ch<='z': ch -= 32Rules to remember
- 1'a'-'A' = 32.
- 2Upper = lower − 32.
- 3Guard with a-z range.
Use this pattern when
- ASCII case manipulation
Interview takeaway
Case conversion is a ±32 ASCII shift behind the library call.
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 case shift. Master it on small inputs and it transfers to harder problems.