Longest Common Suffix
Reported by candidates from Chainalysis's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Chainalysis reported this one in December 2022, and the input size is the first thing to read. Up to 1000 words, 1000 characters each, 100000 characters total. That's small enough that a clean linear scan wins and anything fancy is wasted effort. The hinted pattern says dynamic programming, but this is really a string comparison problem from the back. If you've got the OA in a day or two, learn the shape now. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but this problem is simple enough that you shouldn't need it.
The problem
Given an array of strings words, return the longest string that is a suffix of every word. Return the empty string when there is no common suffix. Function longestCommonSuffix(words: String[]) → String Examples Example 1 words = ["running","jogging","walking"] return = "ing" Example 2 words = ["cat","dog"] return = "" Constraints 1 <= words.length <= 1000. 0 <= words[i].length <= 1000. The total number of characters is at most 100000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: a common suffix of every word can't be longer than the shortest word. So compare characters from the end of all words at once. Take the first word as the reference, then walk index i from 1 upward, checking that every word's i-th character from the right matches the reference. Stop at the first mismatch or when any word runs out. Total work is bounded by the 100000 character cap, so brute force over all suffixes of the first word would also pass, but the backward scan is cleaner. Pitfalls: empty strings in the array (words[i].length can be 0, so the answer is immediately empty), a single-word array (the answer is the whole word), and reversing the result. If you collect characters from the end, you build the suffix backwards, so slice instead. If you freeze on edge cases during the live OA, StealthCoder is the hedge, but the logic is about ten lines.
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 Longest Common Suffix 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
You've seen the question.
Make sure you actually pass Chainalysis's OA.
Chainalysis 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 Common Suffix FAQ
How hard is the Chainalysis Longest Common Suffix question really?+
Easy. It's a string scan with no clever data structure. The only difficulty is handling edge cases like empty strings and a single-word input. If you can write longest common prefix, you can write this by reversing the direction of comparison.
What's the trick to solve it fast?+
Compare characters from the right end of every word at the same offset. Stop at the first mismatch or when any word is too short. The offset where you stop tells you the suffix length, and you slice it from the first word.
Is dynamic programming actually needed here?+
No. The hint says dynamic programming, but nothing overlaps or needs memoization. A simple loop over character positions solves it in O(total characters). Reaching for a DP table just adds code and bugs.
What edge cases should I test before submitting?+
Test an array with one word, an array containing an empty string, words with no shared last letter like cat and dog, and identical words. Also check where one word is itself the suffix of the others, since the shortest word caps the answer.
How do I prepare for this in 48 hours?+
Write longest common prefix, then flip it to suffix, then do it once by reversing each string and once with index math from the end. Time yourself on edge cases. Also review other basic string problems since OAs often pair one easy question with a harder one.