Longest Common File-System Path Prefix
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks most first attempts at this ZipRecruiter problem, reported in September 2022, is comparing raw characters instead of path components. "/ab/c" and "/abc/d" share the characters "/ab", but they share no folder. If you've got an OA coming, this is a string problem in disguise: find the longest common prefix, but the unit is a whole component between slashes. The hinted tag says dynamic programming, but you don't need it. If you blank on the edge cases, StealthCoder runs invisibly during the live assessment and can hand you a clean solution.
The problem
Every input is a canonical absolute Unix path: it starts with /, has no repeated separators, and has no trailing separator unless it is the root. Return the longest path prefix whose complete components are shared by every input. Return / when no named component is common, and return an empty string when paths is empty. Function commonPathPrefix(paths: String[]) → String Examples Example 1 paths = ["/a/b/c","/a/b/d","/a/b"] return = "/a/b" Only complete common components are retained. Example 2 paths = ["/alpha","/beta"] return = "/" Different first components share only the root. Constraints 0 <= paths.length <= 10000 The total path length is at most 200000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to split every path on "/" and compare component by component. Take the first path's components as your reference. For each other path, walk forward while components match, and shorten the reference to the matched length. Rejoin what's left with "/" and prepend a leading "/". If nothing is left, return "/". The pitfalls are all edges. Empty input returns an empty string, not "/". The root path "/" splits into empty pieces, so filter those out. A single path returns itself. Character-level comparison fails on partial names like "/ab" versus "/abc". Complexity is linear in total path length, which fits the 200000 cap easily. No DP table is needed. If the edge cases slip away under the timer, StealthCoder is the hedge that reads the prompt and gives you a working answer the proctor can't see.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Longest Common File-System Path Prefix cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Common File-System Path Prefix FAQ
What's the trick to the ZipRecruiter common path prefix problem?+
Compare whole components, not characters. Split each path on "/", drop empty pieces, and find the longest run of matching components across all paths. Character-level prefixes break on names like "ab" versus "abc", which is exactly the case the examples hint at.
Do I actually need dynamic programming here?+
No. The hinted tag says dynamic programming, but a simple scan works. Keep a running common prefix of components and shrink it against each path. It's a string problem with one pass over the input, so don't build a table.
What edge cases should I test before submitting?+
Test an empty array, which returns an empty string. Test a single path, which returns itself. Test two paths with different first components, which return "/". Test the root path "/" alone. Test names where one is a prefix of another, like "/ab" and "/abc".
How hard is this one really?+
It's easy to medium. The logic is short, but the edge cases catch people. The root path, the empty input, and partial component matches are where first attempts fail. If you handle those three, the rest is routine string work.
How do I prepare for this in 48 hours?+
Write it once from scratch in your language. Split on "/", filter empties, shrink the prefix, and rejoin with a leading slash. Then run both examples plus the empty and root cases by hand. Also review the general longest common prefix pattern, since this is the same idea with a different unit.