Longest Common Prefix
Updated
Problem statement
Given a nonempty array of 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"Every word starts with fl, and the next characters differ.
Example 2
words = ["dog","racecar","car"]return = ""The first characters do not match.
Constraints
1 <= words.length <= 10000.0 <= words[i].length <= 1000.- Every word contains lowercase English letters.
- The total number of characters is at most
100000.
Source note: Author-written prompt excerpt retained for source fidelity.
More Kotak Mahindra Bank Onsite Interview questions
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell Stock IIONSITE INTERVIEW · Seen Nov 2026
- Maximum Non-Adjacent Coins in Two RowsONSITE INTERVIEW · Seen Oct 2026
- Search in Rotated Sorted ArrayONSITE INTERVIEW · Seen Oct 2026
- Find the Index of the First Occurrence in a StringONSITE INTERVIEW · Seen Oct 2026
- Roman to IntegerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Subarrays Whose Sum Is a Power of TwoONSITE INTERVIEW · Seen Oct 2026
- Remove Duplicates From Sorted Array In PlacePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Course ScheduleONSITE INTERVIEW · Seen Sep 2026
- House Robber IIONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Search and Format ContactsAdyen · PHONE SCREEN
- Detect a Straight FlushTemu · PHONE SCREEN
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREEN
- Single-Condition Product FilterCrowdstrike · PHONE SCREEN
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEW
- Bounded Word Guess SolverAtlassian · ONSITE INTERVIEW
- Filter Pins by Excluded IDsPinterest · ONSITE INTERVIEW
- Match a Variadic Function SignatureConfluent · PHONE SCREEN
- Parse and Sort Integer DataLegora · PHONE SCREEN
- Longest Common Parent DirectoryGoogle · ONSITE INTERVIEW