Skip to content
CodeKruPro

Longest common prefix of a list of strings

Coding problemEasyAsked in: Google, Microsoft, VMware

Given a list of strings, return the longest text that every string in the list starts with. If they share no first character, or the list is empty, return an empty string.

Comparison is exact: upper and lower case are different characters, and spaces count.

Examples

Example 1
Input: words = ["flower","flow","flight"]
Output: "fl"
Example 2
Input: words = ["dog","racecar","car"]
Output: ""
Example 3
Input: words = ["interview","internet","interval"]
Output: "inter"

Constraints

  • The list holds between 0 and 1,000 strings
  • Each string has between 0 and 1,000 printable ASCII characters

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in