Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Longest Common Prefix in Strings

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Longest Common Prefix in Strings. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

At Dropbox, file path suggestions may share a common starting segment. Given an array of strings, write a function that returns the longest common prefix shared by all strings.

If there is no common prefix, return an empty string "".

Formal Specification

  • Input: A list of strings strs
  • Output: A string representing the longest prefix common to every string in the list

Notes

A correct solution should compare prefixes efficiently and handle edge cases such as a single string, empty strings, or an immediate mismatch at the first character.

Constraints

  • 1 <= len(strs) <= 200
  • 0 <= len(strs[i]) <= 200
  • strs[i] consists of English letters only

Function Signature

def longest_common_prefix(strs):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output