[etc, tmp] Output: "/etc/tmp" Input: "/usr/bin/./some_file.sh" -> [usr, bin, ., some_file.sh] Output: "/usr/bin/some_file.sh" Input: "/usr/bin/../local" Output: "/usr/local" This problem asks you to normalize a Unix-style file path. The standard solution is to split the path by "/" and process each segment with a stack: push normal directory names, skip ".", and pop for ".." when possible. Finally, join the remaining segments back into a canonical path. The main edge cases are repeated slashes, leading slashes, and attempts to move above the root directory."/> Amazon Online Assessment Interview Question: Simplify a File Path - VO Prep

Amazon Online Assessment Interview Question: Simplify a File Path

19 Views
No Comments

Write a function that takes a string input. The string will be a file path name (for example, /usr/bin). The path may contain special directories . (reference to self) and .. (reference to parent directory). Your function should return the path without using these special directories.

Example input/output

Input: "/etc/tmp" -> [etc, tmp]
Output: "/etc/tmp"

Input: "/usr/bin/./some_file.sh" -> [usr, bin, ., some_file.sh]
Output: "/usr/bin/some_file.sh"

Input: "/usr/bin/../local"
Output: "/usr/local"

This problem asks you to normalize a Unix-style file path. The standard solution is to split the path by "/" and process each segment with a stack: push normal directory names, skip ".", and pop for ".." when possible. Finally, join the remaining segments back into a canonical path. The main edge cases are repeated slashes, leading slashes, and attempts to move above the root directory.

END
 0