Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
If lengths differ, return false. Else compare each index; the first mismatch returns false. In Java use `.equals`, never `==` (which compares references).
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you know Java's == vs .equals distinction and compare correctly.
Where candidates slip
Comparing String references with ==.
How to stand out
Contrast Java (== is reference) with Python (== is value).
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
Character comparison
1Hint 1Explore hintHide hint
What two conditions make strings equal?
2Hint 2Explore hintHide hint
Check length, then compare characters in order.
3Hint 3Explore hintHide hint
In Java, why is `a == b` the wrong way to compare strings?
A · Core idea
Two strings are equal when they have the same length and the same characters in order. Check length first, then compare character by character, returning false on the first mismatch.
The Java `==` trap. In Java, == on String objects compares references (are they the same object?), not contents — so a == b can be false for two equal strings. Always use .equals() for content equality. This is one of the most common fresher bugs.
Why length-check first. Different lengths can't be equal, and the check lets the loop index both strings safely with one bound.
Python. a == b does compare contents, so the language difference itself is worth calling out.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
a = "abc", b = "abd"len
3 == 3
okfalseC · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
The == vs .equals distinction in Java is one of the most common fresher bugs — call it out.
ComplexityO(n) time, O(1) space — length check is O(1), then up to n character comparisons with early exit.
E · Edge cases and pitfalls5a = "abc", b = "abc" — Identical → true.Explore edge casesHide edge cases
Edge cases handled
a = "abc", b = "abc"Identical → true.a = "ab", b = "abc"Length differs → false.a = "", b = ""Both empty → true.
Pitfalls
- Using `==` on Strings in Java (compares references, not contents).
- Skipping the length check.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic boolean equal(String a, String b) { if (a.length() != b.length()) return false; for (int i = 0; i < a.length(); i++) { if (a.charAt(i) != b.charAt(i)) return false; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Using == on Java Strings
== compares object identity; two distinct String objects with the same text are not ==. Use .equals for contents.
Explore the code comparisonHide the code comparison
Using == on Java Strings
== compares object identity; two distinct String objects with the same text are not ==. Use .equals for contents.
Don't write this
if (a == b) return true; // reference comparisonWrite this instead
return a.equals(b); // content comparisonRevise
Carry the pattern into the interview
Pattern to remember
Length + char comparison
Code pattern to memorize
len equal AND every a[i]==b[i] (Java: use .equals)Rules to remember
- 1Length must match.
- 2Compare chars in order.
- 3Java: use .equals, not ==.
Use this pattern when
- Content comparison of strings
Choose another pattern when
- Java == on strings — a reference-comparison bug
Interview takeaway
In Java, == on strings is a reference bug — use .equals.
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: Length + char comparison. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups