Longest Common File-System Path Prefix
Updated
Problem statement
You are given a list of file-system paths, paths. Every path is an absolute Unix path with a clean format. It starts with /, never has two slashes in a row, and does not end with a slash unless the whole path is the root.
Split each path at its slashes into named parts, called components. Find the longest run of leading components that every path shares. Compare whole components only. For example, /a/bc and /a/bd share only /a, not /a/b. Return that shared part as a path.
If the paths share no named component, return /. If paths is empty, return an empty string.
Function
commonPathPrefix(paths: String[]) → StringExamples
Example 1
paths = ["/a/b/c","/a/b/d","/a/b"]return = "/a/b"All three paths start with the components a and b. The third path ends there, and the first two differ next (c and d). So the answer is /a/b.
Example 2
paths = ["/alpha","/beta"]return = "/"The first components are alpha and beta, and they differ. No named component is shared, so the answer is just the root, /.
Constraints
0 <= paths.length <= 10000- The total path length is at most
200000.
More ZipRecruiter Online Assessment questions
- Four-Level Banking System with Scheduled PaymentsOA · Seen Oct 2026
- Count Three-Digit Numbers with Distinct DigitsOA · Seen Oct 2024
- Longest Frequency-Bounded SubarrayOA · Seen Oct 2024
- Minimum Changes to Draw a YOA · Seen Oct 2024
- Apply Binary State OperationsOA · Seen Sep 2024
- Simulate Orchard Rot SpreadOA · Seen Sep 2024
- Compare Odd- and Even-Index SumsOA · Seen Sep 2024
- Coordinate Removals with Column GravityOA · Seen Sep 2024
- Lexicographically Smallest End ReversalOA · Seen Sep 2024
- Recursively Expanded Shell Command CountsOA · Seen Sep 2024
Similar questions at other companies
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Find Words That Can Be Formed by CharactersTabaPay · OA
- Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
- Count Multiples of Three with Two SevensHudson River Trading · OAHudson River Trading interview questions
- Customer Signer ScreeningState Farm · OA
- REST API: Highest International StudentsRippling · OARippling interview questions
- String-Pair Frequency SimilarityIBM · OAIBM interview questions
- Find Sliding-Window Pattern Start IndicesVisa · OAVisa interview questions
- Closest ColorTwo Sigma · OATwo Sigma interview questions
- Number-Character Bijective MappingAdobe · OAAdobe interview questions