Dsa
Stack
Easy
Sticky Path Canonicalizer
You are simplifying a file path. Standard rules apply: '/' is the separator, and '..' moves up one directory. However, some directories are 'sticky'. If a directory name ends with '!', it cannot be removed by a '..' command. The '..' command will simply be ignored if the current directory is sticky.
Input: A single absolute path string.
Output: The canonical path (no trailing slash unless it is the root '/').
Example:
Input: /usr/home!/../bin/../docs
Output: /usr/home!/docs
(Explanation: /usr is pushed. /home! is pushed. '..' tries to pop /home! but fails. /bin is pushed. '..' pops /bin. /docs is pushed.)
Key concepts
stackstring-manipulation
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now