FastPrepLongest Common File-System Path Prefix
Problems/Longest Common File-System Path Prefix

Longest Common File-System Path Prefix

ZipRecruiter logoZipRecruiter● EasyNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A ZipRecruiter new-grad OA report describes splitting file-system paths into parts, comparing the parts position by position, and stopping at the first difference. We supplied the standard Unix format, how the root works, and the result for empty input. Our version is about 88% the same as the reported question.

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[]) → String

Examples

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

ZipRecruiter interview experiences

  • ZipRecruiter SWE OA, Level Not Reported: 23 Experiences
  • ZipRecruiter SWE Intern OA Scores: 4 Experiences
  • ZipRecruiter SWE New Grad OA: 9 Experiences
All ZipRecruiter interview questionsSee ZipRecruiter hiring insights
Saved
class Solution {
    public String commonPathPrefix(String[] paths) {
        // Write your code here.
    }

}
Checking account…