Prompt
Determine if two strings are anagrams (same letters, different order). Case-insensitive. Ignore spaces.
Solution
Anagram checking is the textbook canonicalization pattern: instead of comparing two things directly, map each to a canonical form where equivalent inputs become identical, then compare with ===. Here the canonical form is "lowercased, space-stripped, characters sorted". The same idea powers dedup keys, cache keys, and grouping anagrams into buckets (the follow-up question: use the sorted string as a Map key).
Complexity: the sort dominates — O(n log n) time, O(n) space. The O(n) alternative is a character frequency map: count characters of a up, count characters of b down, verify every count is zero (or compare two histograms). That's asymptotically better and the right answer for the follow-up, but for interview-length strings the sorted-string version is shorter, harder to get wrong, and produces a comparable value — which is exactly why it generalizes to grouping. State the trade-off; don't pretend sort is free.
A cheap pre-check worth mentioning: after cleaning, different lengths can never be anagrams — an early length comparison short-circuits the sort. And note the helper clean is defined once and applied to both inputs: normalize both sides identically or the comparison is meaningless.
Red flag: sorting without normalizing first (case and spaces leak into the comparison), or hand-rolling the frequency compare and forgetting to check for leftover counts — a is "aab", b is "abb" passes a naive "every char of a is in b" check.
Say it: "I canonicalize both strings — normalize, sort, join — and compare; it's O(n log n), and I'd switch to a character histogram for O(n) or for grouping many words by anagram class."