Longest String Chain
Reported by candidates from Visa's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Visa OA question, reported in September 2026, is trying to build chains by comparing every pair of words. With up to 50000 words, that's a timeout waiting to happen. This is Longest String Chain, a dynamic programming problem on a dictionary of lowercase words. You delete one character per step, and every intermediate word has to exist in the dictionary. Duplicates don't count twice. If the pattern doesn't click when the timer's running, StealthCoder is the invisible safety net on your screen during the live OA, so one blank doesn't end the attempt.
The problem
You are given an array of lowercase words words representing a dictionary. A string chain starts with one dictionary word. At each step, remove exactly one character from the current word. The resulting word must also be present in the dictionary. Return the maximum number of words in any valid chain. A single dictionary word forms a chain of length 1. FastPrep practice interpretation: Repeated copies of the same word do not create extra chain positions; membership is determined by distinct word values. Function longestChain(words: String[]) → int Examples Example 1 words = ["a","and","an","bear"] return = 3 The chain ["and", "an", "a"] removes one character at each step and has length 3. Example 2 words = ["a","b","ba","bca","bda","bdca"] return = 4 One longest chain is ["bdca", "bda", "ba", "a"]. Example 3 words = ["abcd","dbqca"] return = 1 Neither word becomes the other by deleting one character, so the longest chain contains one word. Constraints 1 <= words.length <= 50000 1 <= words[i].length <= 60 Every word contains only lowercase English letters from a through z.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: sort words by length, then process shortest to longest. For each word, try deleting each character to build a predecessor. Look that predecessor up in a hash map of word to best chain length. Set dp[word] = max(dp[word], dp[pred] + 1), starting at 1. Track the global max. Cost is roughly n times L squared, where L is at most 60, which is fine for 50000 words. The pitfall is the pairwise comparison approach, which is O(n squared) and dies on large inputs. Another one is forgetting duplicates. A map keyed by word value handles that for free. Also don't assume the chain has to start at a one-letter word. A chain can start at any dictionary word. If you blank on the exact order of processing, StealthCoder can hand you the working solution during the live 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 String Chain 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 string chain. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Visa's OA.
Visa 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 String Chain FAQ
What's the trick for Longest String Chain?+
Sort by length and use a hash map of word to longest chain ending at that word. For each word, delete one character at every position, look up the shorter word, and take the best plus one. Shortest-first order guarantees predecessors are already computed.
How hard is this really?+
Medium. The idea is short once you see it. Most people lose time on the pairwise comparison approach or on mishandling the lookup. If you know sort plus hash map DP, it's about 15 lines of code.
What's the time complexity I should aim for?+
Around O(n * L^2), where L is the max word length of 60. Each word has up to L deletions and each deletion builds a string of length L for the lookup. Pairwise comparison at O(n^2) is too slow for 50000 words.
How do duplicates affect the answer?+
They don't. The problem says repeated copies don't add chain positions, so membership is by distinct value. A hash map keyed by word naturally collapses duplicates. Just don't count the same word twice in a chain.
How do I prepare for this in 48 hours?+
Write the sort plus hash map DP from scratch twice. Test it on the three examples, especially the one where no chain exists and the answer is 1. Then practice similar DP-on-hash-map problems so the pattern feels automatic under a timer.