DSA interview question
A word game awards points when a player rearranges all the letters of one word into another, like "listen" → "silent". The validator must decide whether two strings are anagrams: exactly the same letters with exactly the same counts, in any order. Sorting both strings and comparing works in O(n log n), but because the alphabet is small, a frequency count does it in O(n): add one for each letter of the first string, subtract one for each letter of the second, and check that everything cancels out.
Example: ("anagram", "nagaram") → true; ("rat", "car") → false.
bool isAnagram(const string& s, const string& t); // lowercase English letters
Input (s, t) | Output | Explanation |
|---|---|---|
("anagram", "nagaram") | true | Same letters, same counts |
("rat", "car") | false | t has a c, s has a t |
("a", "ab") | false | Different lengths |
("", "") | true | |
("aacc", "ccac") | false | a appears twice in s but once in t |
("listen", "silent") | true |
(Confirm: lowercase English letters only; a Unicode version would use a hash map instead of an array.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in