0
0
DSA Pythonprogramming~20 mins

Longest Common Prefix in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Longest Common Prefix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Longest Common Prefix Function
What is the output of the following code that finds the longest common prefix among a list of strings?
DSA Python
def longest_common_prefix(strs):
    if not strs:
        return ""
    prefix = strs[0]
    for s in strs[1:]:
        while not s.startswith(prefix):
            prefix = prefix[:-1]
            if not prefix:
                return ""
    return prefix

print(longest_common_prefix(["flower", "flow", "flight"]))
A"flow"
B"f"
C"fl"
D""
Attempts:
2 left
💡 Hint
Check the common starting letters of all strings.
Predict Output
intermediate
2:00remaining
Longest Common Prefix with Empty String Present
What will be the output of this code when one string is empty?
DSA Python
def longest_common_prefix(strs):
    if not strs:
        return ""
    prefix = strs[0]
    for s in strs[1:]:
        while not s.startswith(prefix):
            prefix = prefix[:-1]
            if not prefix:
                return ""
    return prefix

print(longest_common_prefix(["", "b", "c"]))
A"b"
B""
C"c"
DNone
Attempts:
2 left
💡 Hint
If any string is empty, what can the prefix be?
🔧 Debug
advanced
2:00remaining
Identify the Error in Longest Common Prefix Code
What error will this code produce when run?
DSA Python
def longest_common_prefix(strs):
    prefix = strs[0]
    for s in strs[1:]:
        while not s.startswith(prefix):
            prefix = prefix[:-1]
    return prefix

print(longest_common_prefix([]))
AIndexError
BReturns empty string ""
CTypeError
DInfinite loop
Attempts:
2 left
💡 Hint
What happens if strs is an empty list?
🧠 Conceptual
advanced
2:00remaining
Longest Common Prefix Algorithm Complexity
What is the time complexity of the standard longest common prefix algorithm that compares characters one by one among n strings of average length m?
AO(n * m)
BO(n + m)
CO(m^2)
DO(n^2)
Attempts:
2 left
💡 Hint
Consider how many characters are compared in total.
🚀 Application
expert
3:00remaining
Longest Common Prefix in Trie Data Structure
Given a trie built from the strings ["interview", "interrupt", "integrate", "integral"], what is the longest common prefix represented by the trie?
A"integr"
B"int"
C"inter"
D"inte"
Attempts:
2 left
💡 Hint
Find the deepest node in the trie where all strings share the same path.