Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Longest common prefix

Python data engineering interview problem. Difficulty: beginner. Pattern: Strings. About 8 minutes. Part of the Pro drill bank.

Find the longest string that every word starts with. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Implement lcp(words: list[str]) -> str that returns the longest prefix shared by all the words. Return "" when there is no common prefix or when the list is empty.

Requirements

  • Return an empty string when nothing is shared.

Examples

Input: lcp(["flower", "flow", "flight"]) Output: 'fl' All three start with 'fl'; the third letter differs.

Topics: lakebench, python, prefix, strings.

More Python interview questions · All interview problems · Learn data engineering

beginner

Longest common prefix

Interview-style drill: Find the longest string that every word starts with.

Implement `lcp(words: list[str]) -> str` that returns the longest prefix shared by all the words. Return `""` when there is no common prefix or when the list is empty.