Challenge - 5 Problems
Longest Common Prefix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"]))
Attempts:
2 left
💡 Hint
Check the common starting letters of all strings.
✗ Incorrect
The longest common prefix among "flower", "flow", and "flight" is "fl" because all start with these two letters.
❓ Predict Output
intermediate2: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"]))
Attempts:
2 left
💡 Hint
If any string is empty, what can the prefix be?
✗ Incorrect
An empty string means no common prefix can exist other than empty string itself.
🔧 Debug
advanced2: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([]))
Attempts:
2 left
💡 Hint
What happens if strs is an empty list?
✗ Incorrect
The code does not check if strs is empty before accessing strs[0], causing an IndexError.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Consider how many characters are compared in total.
✗ Incorrect
The algorithm compares each character of each string up to the prefix length, resulting in O(n * m) time.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Find the deepest node in the trie where all strings share the same path.
✗ Incorrect
All strings share "inte" as prefix. After that, paths diverge.