Longest Perfect Anagrams
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's 'Longest Perfect Anagrams' showed up in September 2024, and it's a string problem that looks simpler than it is. You're hunting for the longest substring where you can rearrange characters into a palindrome. Most candidates freeze because they don't recognize the pattern fast enough. StealthCoder can feed you the core logic in real time if your mind goes blank under the timer.
Pattern and pitfall
The trick here is that you don't actually need to build palindromes. You need to track character frequency parity using a bitmask or hash set. A substring can form a palindrome if at most one character has an odd count. Use a sliding window or dynamic programming approach where you maintain the XOR state of character frequencies as you scan left to right. The DP angle is subtle: you're building up valid substrings incrementally, checking frequency constraints at each step. Common miss: trying to brute-force all substrings without the parity insight. StealthCoder handles the bitmask optimization so you can focus on the window logic during the OA.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Longest Perfect Anagrams 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 passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as longest palindromic subsequence. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Perfect Anagrams FAQ
Is this really a DP problem or is it sliding window?+
It's both. You can solve it with a sliding window and a frequency map to track parity in linear time. The DP framing means you're building the solution incrementally, checking constraints as you go. Sliding window is usually faster and cleaner for this one.
Do I need to actually build the palindrome?+
No. You only need to check if the characters in a substring CAN form a palindrome. That's a parity check: count odd-frequency characters. If zero or one characters have odd counts, it's valid. You never rearrange anything.
What's the time complexity I should aim for?+
O(n) with a single pass using a bitmask or frequency map to track parity. O(n^2) with nested loops and frequency recalculation is too slow for large inputs. Amazon tests care about linear or near-linear solutions.
How do I handle the bitmask for character frequencies?+
Use XOR. Each bit represents whether a character has odd or even frequency. Flip the bit as you see each character. At the end, count set bits. If count is 0 or 1, the substring forms a valid palindrome.
Should I precompute anything before the main loop?+
Not much. Initialize a hash map or bitmask and iterate through the string once. Store the earliest index where each parity state appears. This lets you calculate substring length without nested loops.