Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Iterate over the characters and increment a counter. (In practice you'd just call `.length()`/`len()`, but the exercise is the manual count.)
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you understand iteration and the code-unit vs character subtlety.
Where candidates slip
Assuming length equals byte count for Unicode.
How to stand out
Distinguish UTF-16 code units from code points / graphemes.
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
Manual count
1Hint 1Explore hintHide hint
Tick a counter once per character as you iterate.
2Hint 2Explore hintHide hint
Empty string is length 0.
3Hint 3Explore hintHide hint
Think about what 'a character' means for multi-byte/emoji text.
A · Core idea
Length is just a character count: step through the string and tick a counter once per character. The exercise is to show you understand what 'length' means rather than to reach for .length().
Why it's a fundamentals check. In real code you call s.length() / len(s) — O(1) in both Java and Python because they store the size. The manual count demonstrates you grasp the underlying iteration.
The Unicode caveat. 'Number of characters' is subtle: String.length() in Java counts UTF-16 code units, so an emoji (a surrogate pair) counts as 2. Counting user-perceived characters requires iterating code points or grapheme clusters. Mentioning this distinction is a strong signal.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "hi"h
+1
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 increment per character; the built-in is O(1) since the size is stored.
E · Edge cases and pitfalls4s = "" — Empty → 0.Explore edge casesHide edge cases
Edge cases handled
s = ""Empty → 0.s = "a b"Spaces count → 3.
Pitfalls
- Counting bytes instead of characters for multi-byte text.
- Off-by-one when looping by index.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int length(String s) { int count = 0; for (int i = 0; i < s.length(); i++) { count++; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Counting bytes instead of characters for multi-byte text.
- Off-by-one when looping by index.
Revise
Carry the pattern into the interview
Pattern to remember
Count by iteration
Code pattern to memorize
count=0; for ch in s: count++Rules to remember
- 1One increment per character.
- 2Empty string → 0.
- 3Normally just use len().
Use this pattern when
- Manual iteration fundamentals
Interview takeaway
Length is a character count — one tick per element.
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: Count by iteration. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups