Anagram Check Go
Fast recall
Quick revision
Core answer
Whether you understand Anagram Check Go beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
The counting solution, executed:
func isAnagram(a, b string) bool {
if len(a) != len(b) { return false }
counts := make(map[rune]int)
for _, r := range a { counts[r]++ }
for _, r := range b { counts[r]-- }
for _, v := range counts {
if v != 0 { return false }
}
return true
}
// isAnagram("listen", "silent") → true — verified
// isAnagram("hello", "world") → false — verifiedThe pattern: count UP for one string, DOWN for the other — if every character cancels (all values zero), the strings have the same multiset of characters. One pass per string + one pass over the counts — O(n).
The alternatives, compared:
- Sort both + compare: O(n log n) — simpler code, slower. Correct, easy to explain, the interview fallback.
- Fixed [26]int for lowercase a-z: O(n), zero allocation — when the alphabet is known-bounded. The map is the general form.
- XOR over bytes: O(n), O(1) — but misses duplicate characters ("aab" vs "abb" XOR to the same value) — a trap, not a solution.
The Unicode consideration: the map[rune]int form is Unicode-correct (é as one rune); the byte form is not; the sort-strings form compares as sorted rune-slices for correctness.
The complexity question the interviewer is really asking: 'can you do it in one pass without sorting?' — the counter is the answer.
Anagram Check Go
Direct Answer
Sort both strings, compare (O(n log n)). Or count runes in a map (O(n), the better answer). If len differs, return false immediately.
Deep Dive & Architecture
The byte-length shortcut's correctness: if len(a) != len(b) (in BYTES), the rune counts can't match — because equal rune counts imply equal total bytes (each rune's UTF-8 width is determined by the rune). The shortcut is safe and skips the loop entirely on mismatches.
The counting map's invariant: after the two loops, counts[r] = (occurrences in a) - (occurrences in b). All zero ⟺ same multiset ⟺ anagram. The single-map trick avoids the two-map comparison (two maps, then check key-set equality — same complexity, more allocations).
The space: the map holds at most min(len(a), distinct runes) entries — O(alphabet), not O(n) — the space is bounded by the character set, not the input length. For ASCII, at most 26-128 entries regardless of million-character inputs.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Anagram Check Go in a real project?
- How would you compare Anagram Check Go with an alternative?
- What is a common mistake when using Anagram Check Go?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.