Reported June 2023
Motivehash table

Palindrome Permutation

Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Motive OA. Under 2s to a working solution.
Founder's read

Rearrange "aab" and you get "aba", a palindrome. Rearrange "code" and nothing works. That's the whole Motive question reported in June 2023, and it's a character-counting problem wearing a palindrome costume. The hint says two-pointers, but you won't need them. Strings run up to 100000 characters, so a single pass is the move. If you've got an invite in your inbox, this one is quick to nail down. And if your mind goes blank mid-assessment, StealthCoder sits invisibly on your screen and hands you the solution in real time.

The problem

Given a lowercase English string s, return whether its characters can be rearranged into a palindrome.

Function
canPermutePalindrome(s: String) → boolean

Examples
Example 1
s = "code"
return = false
Example 2
s = "aab"
return = true

Constraints
0 <= s.length <= 100000.
s contains lowercase English letters only.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: a string can be rearranged into a palindrome only if at most one character has an odd count. Every other letter has to pair up around the center. So count frequencies, then count how many letters have an odd frequency. If that number is 0 or 1, return true. Since the input is lowercase only, a 26-slot array works, or use a hash set that toggles membership on each character. The set size at the end is your odd count. The common pitfall is the empty string. Length 0 is allowed, and it should return true. People also overthink it and try to build the palindrome or sort the characters. Don't. That's wasted work. This runs in O(n) time and O(1) space. If you freeze during the live OA, StealthCoder is the hedge, reading the problem and giving you this solution while the proctor sees nothing.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Palindrome Permutation 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as palindrome permutation. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Motive's OA.

Motive reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Palindrome Permutation FAQ

What's the trick for Palindrome Permutation?+

Count how many characters appear an odd number of times. If that count is 0 or 1, the string can be rearranged into a palindrome. Otherwise it can't. You never need to construct the palindrome, only check the parity of the counts.

Do I actually need two pointers here?+

No. The hinted pattern for this Motive question is two-pointers, but the cleanest solution is a frequency count or a toggling set. Two pointers would only apply if you were checking an already-built palindrome. Counting parity is simpler and runs in one pass.

How hard is this one really?+

Easy. It's a single pass with a counter. The only real risk is overcomplicating it by sorting or generating permutations, which would be far too slow for 100000 characters. If you know the odd-count rule, you can finish in a few minutes.

What edge cases should I test?+

Test the empty string, which should return true. Test a single character, also true. Test "code" for false and "aab" for true. Also try an all-same-letter string and a string with two different odd-count letters, like "abc".

What's the time and space complexity?+

Time is O(n) since you scan the string once. Space is O(1) because the input is lowercase letters only, so a 26-element array or a set capped at 26 entries is enough. That easily handles lengths up to 100000.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Motive.

OA at Motive?
Invisible during screen share
Get it