Longest Common Prefix
Reported by candidates from Wells Fargo's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Wells Fargo OA reported in May 2025 hands you a classic: longest common prefix across up to 10^5 words. The catch is the input size. Word count and word length can each hit 10^5, but the total characters cap at 2 * 10^5, so anything that rescans or compares every pair of words will bleed time. The hinted pattern says dynamic-programming, but this one is really a clean scan. If you've seen it before, it's five minutes of work. If you blank under the timer, StealthCoder runs invisibly on the live OA and gives you the solution as a safety net.
The problem
Given a nonempty array of lowercase strings words, return the longest string that is a prefix of every word. If the words share no nonempty prefix, return the empty string. Function longestCommonPrefix(words: String[]) → String Examples Example 1 words = ["flower","flow","flight"] return = "fl" All three words begin with fl, but their third characters differ. Example 2 words = ["dog","racecar","car"] return = "" The first characters are not all equal. Example 3 words = ["interview","internet","internal"] return = "inter" The shared prefix ends before the sixth character. Constraints 1 <= words.length <= 10^5. 1 <= words[i].length <= 10^5. The sum of all string lengths is at most 2 * 10^5. Every string contains lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to stop at the first mismatch. Take the first word as the reference. Walk column by column, index i from 0 up to its length. For each i, check every other word: if i is past its end or its character differs, return the prefix built so far. Total work is bounded by the sum of lengths, so 2 * 10^5 character checks at worst. That's fine. The pitfall is the pairwise approach, comparing every word to every other word, or building substrings repeatedly inside a loop. Another common slip is forgetting the early exit when a word is shorter than the current index, which throws an out-of-bounds error. Sorting and comparing only the first and last word also works, but costs extra. Don't overthink the DP hint. If you freeze mid-assessment, StealthCoder is the hedge that reads the problem and hands you the column-scan version.
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 Common 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. 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 common prefix. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Wells Fargo's OA.
Wells Fargo 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 Common Prefix FAQ
How hard is Longest Common Prefix really?+
It's an easy problem. The logic is a single loop with an early exit. The Wells Fargo version just adds big constraints to punish sloppy pairwise comparisons. If you write the column scan cleanly, you're done in a few minutes with edge cases handled.
What's the trick for the large input size?+
The sum of all string lengths is at most 2 * 10^5, so a scan that stops at the first mismatch is linear in total characters. Never compare every pair of words and never rebuild substrings in a loop. Use the first word as the reference and check the same index in each other word.
Is dynamic programming needed here?+
No. The hinted pattern says dynamic-programming, but the problem has no overlapping subproblems. A vertical scan, a horizontal reduction, or sorting and comparing the first and last word all work. Pick the scan because it's simplest and hardest to get wrong.
What edge cases should I test?+
Test a single word, which should return itself. Test words with no shared first character, which returns an empty string. Test one word that's a prefix of the others, like inter and interview. Also test a shorter word mid-array so your index check doesn't go out of bounds.
How do I prepare in 48 hours?+
Write this one from memory twice, then do two or three other string scan problems with early exits. Focus on clean loops and boundary checks, not new algorithms. Time yourself so the pattern feels automatic before you open the Wells Fargo assessment.