DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
easy
String

Longest Common Prefix

**Problem Statement:** Given the required input arguments, write an efficient algorithm to solve the **Longest Common Prefix** problem. Implement the required logic as specified by standard definitions for this classic algorithmic challenge. **Hint / Expected Approach:** Vertical scan or sort + compare first/last **Edge Cases to Consider:** - (1) Empty array - (2) No common prefix - (3) One string is prefix of another

Examples

Example 1
Input: ["flower","flow","flight"]
Output: "fl"
Example 2
Input: ["dog","racecar","car"]
Output: ""
Explanation: No common prefix
Example 3
Input: ["a"]
Output: "a"
Explanation: Single string

Constraints

  • ▪1 <= s.length <= 10^4
  • ▪s consists of printable ASCII characters
  • ▪Solve with optimal space complexity

Watch Out For Edge Cases

  • ▪Empty array
  • ▪No common prefix
  • ▪One string is prefix of another
Frequently Asked At
AmazonMicrosoftGoogleAdobeTCSInfosys