Meta Online Coding Assessment: Simplify Unix File Path
Question Description
You're asked to implement a function that takes an absolute Unix-style path and returns its canonical simplified path. The input always begins with a leading slash (/), and your result must start with a single slash, separate directory names with one slash, remove "." components, resolve ".." to move up one level, collapse consecutive slashes, and never end with a slash unless the result is root (/).
A practical approach is to parse the string and process components left-to-right. Split on "/" (or scan and extract segments), then use a stack (or list) to:
- ignore empty segments and single-period (".") segments
- pop the stack for double-period ("..") segments when possible
- push any valid directory name (including names like "...")
Finally, join the stack with "/" and prefix a leading slash (or return "/" if the stack is empty).
Skill signals interviewers look for: robust string parsing, correct handling of edge cases (multiple slashes, trailing slashes, sequences of periods that are valid names), use of a stack or deque, and time/space complexity reasoning. Expect the interviewer to ask for complexity (O(n) time, O(n) extra space) and to probe alternative implementations (in-place string modification, one-pass parsing, or handling relative paths and symbolic links). Test your code with many edge cases and sample paths before submitting.
Common Follow-up Questions
- •How would you adapt your simplify_path implementation to accept relative paths given a current working directory?
- •Can you implement the solution in-place (O(1) extra space) or with a single scan without splitting the string? Show trade-offs.
- •How would you extend canonicalization to resolve symbolic links (symlinks) and file-system-aware rules?
- •What changes if you must preserve and validate directory name lengths or forbid certain characters? How does that affect parsing?
- •How would you modify the algorithm to run efficiently on extremely long paths or streams (memory-constrained environments)?
Related Questions
Explore More Questions
Practice This Question with AI
Get real-time hints, detailed requirements, and insightful analysis of the question.