How would you find duplicates or compare character frequencies efficiently?
Answer
Walk the input once and store each value or normalized character in a set or frequency map. A set is enough to detect whether a duplicate exists; counts are needed to report duplicates or compare multisets such as anagrams. Clarify case, whitespace and Unicode rules, then test empty input, repeated values, equivalent normalized text and inputs with no duplicates.
Strong answer includes
- selects a set or frequency map for the required output
- states normalization assumptions
- covers empty, unique and repeated inputs
Practical example
Given the strings 'listen' and 'silent' for an anagram check, a candidate who only compares lowercase letters misses that 'Silent ' with a trailing space and different casing should still count as equivalent once the interviewer clarifies that whitespace and case should be normalized before comparison.