Your Dashboard
Interview Coaching
Learn
System Design
ML System Design
Code
Behavioral
Salary Negotiation
Interview Guides
Unix Cd Command with Symbolic Link Resolution
Simulate the Unix cd command by implementing path resolution for relative paths ('.' and '..'), absolute paths, and symbolic links while detecting potential cycles. Use stack-based directory traversal for path management and depth-first search to handle symbolic link mappings and cycle detection. Handle edge cases including redundant slashes and invalid path references.
Asked at:
OpenAI
Meta
DESCRIPTION
Simulate the behavior of the Unix cd (change directory) command by implementing path resolution for navigating a file system. The program must correctly interpret relative paths, absolute paths, and symbolic links, while ensuring cycle detection for symbolic links that may form loops.
Input:
Output:
Explanation: First 'boo' is appended, '.' means stay (no change), then '..' goes up one level, then 'doe' is appended
Constraints:
- Paths are Unix-style with forward slashes
- Absolute paths start with '/', relative paths do not
- '..' means parent directory, '.' means current directory
- Multiple consecutive slashes should be treated as a single slash
- Symbolic links may exist and must be resolved
- Cycles in symbolic links must be detected and handled
- Cannot go above root directory (/.., /../../ etc. should resolve to /)
Understanding the Problem
Let's understand what we're being asked to do. We have a current working directory (like /foo/bar) and a cd command (like ../boo or /boo), and we need to compute the final path after executing the command.
Tracing through examples:
- From /foo/bar, command boo → append to current path → /foo/bar/boo
- From /foo/bar, command /boo → absolute path (starts with /) → /boo
- From /foo/bar, command ../boo → go up one level (..), then down to boo → /foo/boo
- From /foo/bar, command boo/././doe → append boo, . means stay (no change), . again (no change), then doe → /foo/bar/boo/doe
Important constraints: We need to handle symbolic links (shortcuts that point to other directories) and detect cycles (when following links creates an infinite loop). We also need to handle redundant slashes like //foo///bar (should be treated as /foo/bar).
Edge cases to consider: What if we try to go up from root (/..)? What if the path has trailing slashes (/foo/bar/)? What if a symbolic link points to itself or creates a cycle? What if the command is just . or ..?
Building Intuition
A file path is essentially a sequence of directory names separated by slashes. We can break the path into components and process them one by one.
Special components like . (current directory) and .. (parent directory) modify our position in the directory tree. The key insight is that .. means 'go back one level' - this is a last-in, first-out operation where we need to undo the most recent directory we entered.
By treating the path as a sequence of operations (enter directory, go back, stay), we can use a data structure that naturally supports adding and removing from the end.
For symbolic links, we need to track which paths we've visited while resolving links. If we encounter the same path twice, we've found a cycle and should stop to avoid infinite loops.
Think of navigating directories like building a tower of blocks:
- Each directory name adds a block to the top
- The .. command removes the top block (go back one level)
- The . command does nothing (stay at current level)
- An absolute path (starting with /) means clear all blocks and start fresh
For symbolic links, imagine following a trail of breadcrumbs. If you ever see the same landmark twice, you're walking in circles and need to stop.
Common Mistakes
Optimal Solution
Use a stack-based approach to manage directory traversal. Split the path into components by '/' delimiter. For each component: if it's a regular directory name, push it onto the stack; if it's '..', pop from the stack (go up one level); if it's '.', do nothing (stay at current level). Handle absolute paths by clearing the stack first. For symbolic links, use depth-first search with a visited set to track paths and detect cycles. Join the final stack contents with '/' to produce the resolved path.
Start cd command simulation
0 / 26
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n + m) Where n is the length of currentPath and m is the length of command. We process each character once when splitting and building the path. For symbolic links, DFS visits each unique path at most once.
Space Complexity: O(n + m) The stack stores directory components (O(n) for depth of path). The visited set for cycle detection stores paths we've seen (O(m) in worst case). The final path string is O(n + m).
What We've Learned
- Stack-based path traversal: Use a stack to manage directory components when processing paths - push directories onto the stack and pop for '..' operations. This naturally handles nested directory structures and makes backtracking trivial, perfect for any hierarchical navigation problem.
- Tokenization before processing: Split paths by '/' delimiters and process each component individually rather than manipulating the raw string. This cleanly handles edge cases like redundant slashes ('/foo//bar'), trailing slashes, and makes '.' and '..' logic straightforward to implement.
- Cycle detection with visited set: When resolving symbolic links, maintain a visited set of paths encountered during the current resolution chain. Before following a symlink, check if the target is already in the set - this prevents infinite loops in circular symlink references.
- Absolute vs relative path handling: Always check if a path starts with '/' to determine if it's absolute or relative. For absolute paths, clear your stack and start fresh; for relative paths, build upon the current directory stack. Missing this distinction causes incorrect path resolution.
- Normalization through reconstruction: After processing all path components with the stack, reconstruct the final path by joining stack elements with '/' and prepending '/'. This ensures a canonical format regardless of input variations (multiple slashes, dots, etc.).
- Real-world file system operations: This pattern directly applies to implementing file system utilities, URL path resolution in web servers, package dependency resolution, and any system requiring hierarchical namespace navigation with shortcuts or aliases.
Related Concepts and Problems to Practice
medium
This problem involves parsing strings with nested structures and using a stack to manage state during traversal, similar to how the cd command must parse path components and maintain directory state. Both require handling special characters and nested contexts.
medium
This problem requires cycle detection in a graph structure using DFS, which directly relates to detecting cycles in symbolic link resolution. Both problems involve traversing connected structures while tracking visited nodes to prevent infinite loops.
Test Your Understanding
Why is stack the right data structure for this problem?
stack provides the optimal access pattern
It's the only data structure that works
It's the easiest to implement
It uses the least memory
Select an answer to see the explanation
Question Timeline
See when this question was last asked and where, including any notes left by other candidates.
Late September, 2025
OpenAI
Staff
Early August, 2025
Meta
Senior
Given the present working directory and the cd path, return the final path e.g. pwd: /foo/bar cd: boo -> /foo/bar/boo pwd: /foo/bar cd: /boo -> /boo pwd: /foo/bar cd: ../boo -> /foo/boo pwd: /foo/bar cd: boo/././doe -> /foo/bar/boo/doe
Mid June, 2025
Meta
Staff
Comments
Hello Interview Premium
Your account is free and you can post anonymously if you choose.