Reported September 2026
Amazonstring

Longest Happy Prefix

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

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

The Amazon OA reported in September 2026 asks for the longest happy prefix, and the whole thing hinges on one array: the KMP failure table, also called the LPS array. If you've never built one, this problem will feel like a trap. If you have, it's about ten lines. You're given a string up to 100000 characters, so any approach that compares every prefix to every suffix will time out. Hedge for the live OA: StealthCoder runs invisibly on your desktop and reads the problem for you if your mind goes blank on the table logic.

The problem

A string is a happy prefix of s when it is a non-empty proper prefix of s and is also a suffix of s.
Return the longest happy prefix. If none exists, return the empty string.

Function
longestPrefix(s: String) → String

Examples
Example 1
s = "level"
return = "l"
The prefix l is also the suffix, and no longer proper prefix matches.
Example 2
s = "ababab"
return = "abab"
The first four characters equal the last four characters.
Example 3
s = "a"
return = ""
A one-character string has no non-empty proper prefix.

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

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is the prefix function. For each index i, lps[i] stores the length of the longest proper prefix of s[0..i] that is also a suffix of it. The answer is s[0:lps[n-1]]. Build it with one pointer j. For each i from 1, while j > 0 and s[i] != s[j], set j = lps[j-1]. If they match, increment j. Then set lps[i] = j. That's O(n) time and O(n) space. The common pitfall is the fallback step. Candidates reset j to 0 instead of jumping to lps[j-1], which breaks on strings like ababab. Another trap is returning the whole string, since the prefix must be proper. Example 3 with a single character should give the empty string. A rolling hash also works but risks collisions. If the table logic slips away mid-assessment, StealthCoder is the safety net that surfaces a working solution without the proctor seeing anything.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Longest Happy Prefix 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 StealthCoder

Related leaked OAs

⏵ The honest play

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

Amazon 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.

Longest Happy Prefix FAQ

What's the trick to Longest Happy Prefix?+

Build the KMP prefix function. lps[i] is the longest proper prefix of s[0..i] that's also a suffix. The answer is the first lps[n-1] characters of s. One pass, O(n) time. Don't compare prefixes and suffixes directly, that's O(n^2) and fails at 100000 characters.

How hard is this really for an Amazon OA?+

It's a hard-rated problem if you've never seen KMP, and easy if you have. The code is short, but the fallback logic is easy to get wrong. Most failures come from not knowing the prefix function, not from implementation size.

Can I use hashing instead of KMP?+

Yes. Compute rolling hashes for each prefix and suffix of the same length and track the longest match, checking lengths from n-1 down to 1. It works in O(n), but collisions are a risk, so use a large modulus or double hash. KMP is deterministic and safer.

What edge cases should I test?+

Test a single character like a, which returns the empty string. Test all the same letters like aaaa, which returns aaa. Test ababab, which returns abab, where prefix and suffix overlap. Test a string with no match like abc, which returns empty. Never return the full string.

How do I prepare in 48 hours?+

Write the prefix function from memory three times on different strings. Trace ababab and aabaaab by hand to see how j falls back. Then solve it cold with a timer. Focus on the while-loop fallback, since that's the part that breaks under pressure.

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

OA at Amazon?
Invisible during screen share
Get it