Valid Anagram
Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Two lowercase strings, up to 100000 characters each, and one question: is t a rearrangement of s? That's the Valid Anagram problem Motive candidates reported in June 2023. It's a hash-table counting problem, and it's one of the friendliest you'll see on an OA. The danger isn't difficulty, it's overthinking or tripping on an edge case like empty strings or unequal lengths. If you've got an invite for Motive, expect to solve this in a few minutes. StealthCoder sits invisibly on your screen as a safety net in case your mind goes blank mid-assessment, but the idea here is simple enough to hold in your head.
The problem
Given two lowercase English strings s and t, return true if t is an anagram of s, and false otherwise. Function isAnagram(s: String, t: String) → boolean Examples Example 1 s = "anagram" t = "nagaram" return = true Example 2 s = "rat" t = "car" return = false Constraints 0 <= s.length, t.length <= 100000. Both strings contain lowercase English letters only.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is counting. Two strings are anagrams if every letter appears the same number of times in both. Since the input is lowercase English only, use an array of 26 integers. Walk s and increment, walk t and decrement, then check that every slot is zero. Check lengths first and return false early if they differ. That's O(n) time and O(1) space. The common pitfalls: sorting both strings works but costs O(n log n), which is fine here but looks weaker. Another miss is forgetting the empty case. Two empty strings are anagrams, so return true. Don't compare only sets of characters, because that ignores frequency. Use a map instead of an array only if the constraints loosen to unicode. If you freeze during the live OA, StealthCoder can surface the counting solution in seconds, but you should be able to write this from memory.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Valid Anagram cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as valid anagram. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Motive's OA.
Motive reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Valid Anagram FAQ
How hard is Valid Anagram really?+
It's easy. The Motive version is the classic frequency-count problem. If you know to count letters and compare, you're done in under ten lines. The only real risk is sloppy edge cases like different lengths or empty inputs.
What's the trick to solve it fast?+
Use an int array of size 26. Increment for each char in s, decrement for each char in t, then confirm all zeros. Return false immediately if the lengths differ. It's linear time and constant space.
Should I sort the strings instead?+
Sorting both and comparing works and is correct. It's O(n log n) versus O(n) for counting. With lengths up to 100000, both pass. Counting is cleaner and shows you know the better approach, so lead with that.
What edge cases should I test?+
Test two empty strings (true), strings of different lengths (false), identical strings (true), and same letters with different counts like aab versus abb. Constraints allow length 0, so don't assume at least one character.
How do I prepare in 48 hours for an OA like this?+
Get comfortable with frequency counting using arrays and hash maps. Write this problem plus a couple of siblings, like group anagrams and ransom note, from scratch once. Focus on clean code and edge cases, not on memorizing exotic algorithms.