As reported: Wells Fargo Technology Summer Analyst Intern, on-campus online assessment (India, 2025), three DSA problems. The medium one: return the longest common prefix of an array of strings.
This is LeetCode 14 — Longest Common Prefix.
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: strs = ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Constraints:
- 1 <= strs.length <= 200
- 0 <= strs[i].length <= 200
- strs[i] consists of only lower-case English letters.
Code runner format: one JSON value per line, in argument order (strs), as in LeetCode's test cases. Implement longestCommonPrefix; the harness prints the result as compact JSON (sorted first when any order is accepted).