Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
For each char: if uppercase add 32, if lowercase subtract 32, else leave it. XOR with 32 does both at once for letters.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you can flip case and know the XOR-32 bit trick.
Where candidates slip
Forgetting to guard non-letters, or only handling one direction.
How to stand out
Explain why XOR 32 works (cases differ only in bit 5).
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 case flip
1Hint 1Explore hintHide hint
Toggle = uppercase-shift for lowercase letters and vice versa.
2Hint 2Explore hintHide hint
Which single bit differs between 'A' and 'a'?
3Hint 3Explore hintHide hint
`ch ^ 32` flips case for letters in one op — but guard non-letters.
A · Core idea
Flip each letter's case: uppercase becomes lowercase and vice versa, leaving non-letters alone. The elegant trick is XOR with 32.
Why `ch ^ 32` flips case. Upper- and lowercase ASCII letters differ in exactly one bit — bit 5 (value 32). 'A' is 0b1000001 and 'a' is 0b1100001. XOR-ing with 32 toggles that bit, switching 'A'↔'a' in a single operation, for any letter.
Why guard to letters. XOR-ing 32 into a digit or symbol would corrupt it, so only apply the flip to characters in a–z or A–Z. The explicit +32/−32 branch version makes the intent clear; the XOR is the slick alternative to mention.
Toggle is just the union of the upper-shift and lower-shift problems.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "aB1"a
lower → upper
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
ch ^ 32 flips case for ASCII letters because upper/lower differ only in bit 5.
ComplexityO(n) time, O(n) space — one branch (or XOR) per character.
E · Edge cases and pitfalls5s = "Hello" — → "hELLO".Explore edge casesHide edge cases
Edge cases handled
s = "Hello"→ "hELLO".s = "XYZ"→ "xyz".s = "123"Unchanged.
Pitfalls
- Flipping non-letters.
- Handling only one of the two cases.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String toggle(String s) { char[] c = s.toCharArray(); for (int i = 0; i < c.length; i++) { if (c[i] >= 'a' && c[i] <= 'z') c[i] -= 32; else 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.
- Flipping non-letters.
- Handling only one of the two cases.
Revise
Carry the pattern into the interview
Pattern to remember
ASCII case flip / XOR 32
Code pattern to memorize
if isLetter(ch): ch ^= 32Rules to remember
- 1Upper +32, lower −32.
- 2
ch ^ 32flips letters. - 3Skip non-letters.
Use this pattern when
- Case flipping
- Bit-level case tricks
Interview takeaway
Letters differ by bit 5 — XOR 32 toggles case.
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 flip / XOR 32. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups