Practice/Meta/Unix Cd Command with Symbolic Link Resolution
CodingMust
You are building a file system navigation utility that simulates changing directories similar to the Unix cd command. Your implementation must handle absolute paths, relative paths with special directory references (. for current and .. for parent), and symbolic links that redirect to other paths in the file system.
Given a current working directory path, a target path to navigate to, and a mapping of symbolic links, determine the final absolute path after resolving all navigation operations and symlink redirections. If a circular symlink reference is detected, return the string "CYCLE_DETECTED".
/) by replacing the current path entirely. (current directory) and .. (parent directory) references correctly"CYCLE_DETECTED"/)Example 1:
Input: current_path = "/home/user" target_path = "docs/projects" symlinks = \{\} Output: "/home/user/docs/projects" Explanation: Relative path appended to current directory with no symlinks
Example 2:
Input: current_path = "/a/b/c" target_path = "../../d" symlinks = \{\} Output: "/a/d" Explanation: Navigate up two levels from /a/b/c to /a, then down into d
Example 3:
Input: current_path = "/usr" target_path = "local/bin" symlinks = \{"/usr/local": "/opt/custom"\} Output: "/opt/custom/bin" Explanation: Path /usr/local is a symlink to /opt/custom, so final path becomes /opt/custom/bin
Example 4:
Input: current_path = "/home" target_path = "link/file" symlinks = \{"/home/link": "/tmp/other", "/tmp/other": "/home/link"\} Output: "CYCLE_DETECTED" Explanation: Circular reference detected between /home/link and /tmp/other