FastPrepLongest Common Prefix
Problems/Longest Common Prefix

Longest Common Prefix

Kotak Mahindra Bank logoKotak Mahindra Bank● EasyFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Two reports name this same task and give the full goal. We chose the input limits and the function signature. Our version is about 99% the same as the reported questions.

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[]) → String

Examples

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
See Kotak Mahindra Bank hiring insights
Saved
class Solution {
    public String longestCommonPrefix(String[] words) {
        // Write your code here.
    }
}
Checking account…