Valid Anagram
Python data engineering interview problem. Difficulty: beginner. Pattern: Hashing. About 10 minutes. Free to practice.
Check whether two strings are anagrams of each other. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
2 words are anagrams when they use exactly the same letters, the same number of times, only in a different order. 'listen' and 'silent' are anagrams. Write a function valid_anagram(s, t) that returns True if t is an anagram of s, and False otherwise. For 'anagram' and 'nagaram' the answer is True. Capital and small letters are different characters, so 'A' is not the same as 'a'. Keep the print lines in the editor.
Requirements
- Return True only if both strings have the same letters with the same counts.
- Return False right away if the lengths are different.
- Treat uppercase and lowercase letters as different.
Constraints
- The comparison is case-sensitive.
- Strings with different lengths can never be anagrams.
Examples
Input: valid_anagram('anagram', 'nagaram') Output: True Same letters rearranged.
Topics: lakebench, python, anagram, counting.
More Python interview questions · All interview problems · Learn data engineering
Valid Anagram
Interview-style drill: Check whether two strings are anagrams of each other.
Implement `valid_anagram(s: str, t: str) -> bool`. Example: `"anagram", "nagaram"` → `True`. Keep the harness.