Longest Common Prefix
Updated
Problem statement
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[]) → StringExamples
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.
More Wells Fargo Online Assessment questions
- Server Upgrade PlanningOA · Seen Nov 2026
- Student and Course EnrollmentOA · Seen Nov 2026
- Bond Interest Summary Above 3%OA · Seen Oct 2026
- Count Phrases Formed by Anagram ReplacementOA · Seen Oct 2026
- Event Statistics AggregatorOA · Seen Sep 2026
- Ordered Deployment Configuration ValidatorOA · Seen Sep 2026
- Longest Subsequence That Is a SubstringOA · Seen Jul 2026
- Overloaded Game Account InventoriesOA · Seen Jul 2026
- Count Server ReplacementsOA · PHONE SCREEN · Seen Feb 2026
- Minimum Remaining LengthOA · PHONE SCREEN · Seen Feb 2026
Similar questions at other companies
- Find a String Within One Mismatch of AllBarclays · OABarclays interview questions
- Count Subarrays Matching a Comparison PatternTikTok · OATikTok interview questions
- Earliest Time to Schedule a MeetingMathWorks · OAMathWorks interview questions
- Earliest Meeting Start on a 24-Hour ClockIMC Trading · OAIMC Trading interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Count One-Swap Number PairsCapital One · OACapital One interview questions
- Earliest Time to Schedule a MeetingMicrosoft · OAMicrosoft interview questions
- Nested Recipe Blocks (Game Adaptation)Roblox · OARoblox interview questions
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Maximum Equal Parts for PrefixesAmazon · OA