EasyStringsAI interview only
Longest Common Prefix
Asked atgoogleamazonappleadobemicrosoft
01 · Problem
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 "".
02 · Examples
Example 01
Input
strs = ["flower","flow","flight"]
Output
"fl"
All three strings start with "fl". The third string diverges at index 2 ('i' vs 'o'), so the longest common prefix is "fl".
Example 02
Input
strs = ["dog","racecar","car"]
Output
""
There is no character that all three strings share at index 0 ('d' vs 'r' vs 'c'), so the common prefix is empty.
03 · Constraints
- 011 <= strs.length <= 200
- 020 <= strs[i].length <= 200
- 03strs[i] consists of only lowercase English letters.
04 · Optimal complexity
- Time
- O(n * m)
- Space
- O(1)
05 · Two ways to work on it
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.