Longest Common Prefix is the #15 most asked LeetCode problem globally — a clean string problem that tests methodical thinking and knowledge of multiple valid approaches.
Our analysis of 10,385 verified interview questions across 259 companies shows it appeared 62 times across 24 companies — including Google, Amazon, Meta, Microsoft, Bloomberg, Jane Street, CME Group, Deutsche Bank, HSBC, and Disney.
There are four valid approaches — horizontal scan, vertical scan, sort + compare ends, and Trie. The simplest and most interview-friendly is horizontal scan: use the first string as the initial prefix and shrink it until it matches every other string. Clean, O(n·m), and easy to explain.
| Metric | Value |
|---|---|
| Total Interview Appearances | 62 verified mentions |
| Companies That Ask It | 24 companies |
| Global Rank | #15 out of 10,385 questions |
| Difficulty | Easy |
| Core Patterns | String, Trie |
Google, Amazon, Meta, Microsoft, Bloomberg, Jane Street, CME Group, Deutsche Bank, HSBC, Disney, Adobe, Apple, Capgemini, Cognizant, Deloitte, DXC Technology, EPAM Systems, HCL, IBM, Infosys, and 4 more.
LeetCode #14 — Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "".
Input: strs = ["flower","flow","flight"] → Output: "fl"
Input: strs = ["dog","racecar","car"] → Output: ""
Input: strs = ["ab","a"] → Output: "a"
| Approach | Idea | Time | Space |
|---|---|---|---|
| Horizontal Scan | Start with strs[0] as prefix, shrink until matches each string | O(n·m) | O(1) |
| Vertical Scan | Compare column by column across all strings | O(n·m) | O(1) |
| Sort + Compare Ends | Sort array; compare only first and last string | O(n log n) | O(1) |
| Trie | Insert all strings, traverse common path | O(n·m) | O(n·m) |
Horizontal scan for simplicity and O(1) space. Mention sort + compare as an elegant alternative (sort alphabetically — the longest common prefix of all strings must be a prefix of both the lexicographically smallest and largest string). Know Trie if the interviewer asks about building an autocomplete system.
def longestCommonPrefix(strs: list[str]) -> str:
prefix = strs[0]
for s in strs[1:]:
# Shrink prefix until s starts with it
while not s.startswith(prefix):
prefix = prefix[:-1]
if not prefix:
return ""
return prefix
def longestCommonPrefix(strs: list[str]) -> str:
strs.sort()
first, last = strs[0], strs[-1]
i = 0
while i < len(first) and i < len(last) and first[i] == last[i]:
i += 1
return first[:i]
class Solution {
public String longestCommonPrefix(String[] strs) {
String prefix = strs[0];
for (int i = 1; i < strs.length; i++) {
while (!strs[i].startsWith(prefix)) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()) return "";
}
}
return prefix;
}
}
var longestCommonPrefix = function(strs) {
let prefix = strs[0];
for (let i = 1; i < strs.length; i++) {
while (!strs[i].startsWith(prefix)) {
prefix = prefix.slice(0, -1);
if (!prefix) return "";
}
}
return prefix;
};
Tag on DSAPrep.dev under String and Trie.