Challenge - 5 Problems
Longest Common Prefix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Longest Common Prefix Function
What is the output of the following Rust code that finds the longest common prefix of a list of strings?
DSA Rust
fn longest_common_prefix(strs: Vec<&str>) -> String { if strs.is_empty() { return String::new(); } let mut prefix = strs[0].to_string(); for s in &strs[1..] { while !s.starts_with(&prefix) { prefix.pop(); if prefix.is_empty() { return String::new(); } } } prefix } fn main() { let words = vec!["flower", "flow", "flight"]; let result = longest_common_prefix(words); println!("{}", result); }
Attempts:
2 left
💡 Hint
Check the common starting letters of all words until a mismatch occurs.
✗ Incorrect
The longest common prefix among "flower", "flow", and "flight" is "fl" because all three start with 'f' and 'l', but differ after that.
❓ Predict Output
intermediateLongest Common Prefix with Empty String Present
What will be printed by this Rust program that finds the longest common prefix when one string is empty?
DSA Rust
fn longest_common_prefix(strs: Vec<&str>) -> String { if strs.is_empty() { return String::new(); } let mut prefix = strs[0].to_string(); for s in &strs[1..] { while !s.starts_with(&prefix) { prefix.pop(); if prefix.is_empty() { return String::new(); } } } prefix } fn main() { let words = vec!["dog", "", "dove"]; let result = longest_common_prefix(words); println!("{}", result); }
Attempts:
2 left
💡 Hint
An empty string means no common prefix longer than empty string.
✗ Incorrect
Since one string is empty, the longest common prefix must be empty as no characters are common at the start.
🧠 Conceptual
advancedBehavior of Longest Common Prefix Algorithm
Which statement best describes how the longest common prefix algorithm works in the given Rust code?
DSA Rust
fn longest_common_prefix(strs: Vec<&str>) -> String { if strs.is_empty() { return String::new(); } let mut prefix = strs[0].to_string(); for s in &strs[1..] { while !s.starts_with(&prefix) { prefix.pop(); if prefix.is_empty() { return String::new(); } } } prefix }
Attempts:
2 left
💡 Hint
Look at how the prefix variable is updated inside the loop.
✗ Incorrect
The code initializes prefix as the first string, then shortens it by removing last characters until all strings start with it.
🔧 Debug
advancedIdentify the Error in Longest Common Prefix Code
What error will this Rust code produce when trying to find the longest common prefix?
DSA Rust
fn longest_common_prefix(strs: Vec<&str>) -> String { if strs.is_empty() { return String::new(); } let mut prefix = strs[0].to_string(); for s in &strs[1..] { while !s.starts_with(prefix) { prefix.pop(); if prefix.is_empty() { return String::new(); } } } prefix }
Attempts:
2 left
💡 Hint
Check the argument type passed to starts_with method.
✗ Incorrect
starts_with expects a string slice (&str), but prefix is a String. Passing prefix directly causes a type mismatch error.
🚀 Application
expertLongest Common Prefix Result for Large Input
Given a vector of 100,000 strings where each string is "abcde" repeated 10 times, except the last string which is "abcdf" repeated 10 times, what is the longest common prefix returned by the Rust function?
DSA Rust
fn longest_common_prefix(strs: Vec<&str>) -> String { if strs.is_empty() { return String::new(); } let mut prefix = strs[0].to_string(); for s in &strs[1..] { while !s.starts_with(&prefix) { prefix.pop(); if prefix.is_empty() { return String::new(); } } } prefix } fn main() { let mut words = vec!["abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde"; 99999]; words.push("abcdfabcdfabcdfabcdfabcdfabcdfabcdfabcdfabcdfabcdf"); let result = longest_common_prefix(words); println!("{}", result); }
Attempts:
2 left
💡 Hint
Check where the first mismatch occurs between the last string and others.
✗ Incorrect
The last string differs at the 5th character ('e' vs 'f'), so the common prefix stops before that character, resulting in "abcd".
