Meta Interview Problem #1 · Normalize Path (simulate cd)

Problem Statement (English original)
You are given a current working directory cwd (an absolute Unix-style path) and a cd(arg) string which can be either an absolute path (starts with /) or a relative path (may include segments . and ..).
Implement a function that returns the normalized absolute path after applying cd(arg) from cwd.

Rules:

  • Path separator is /.
  • . means“stay in the same directory”.
  • .. means“go to parent directory”(but never above root).
  • Collapse multiple slashes and remove trailing slash (except for root /).
  • The output must always be an absolute path.

Examples (from the prompt):

cwd cd(arg) output
/ foo /foo
/baz /bar /bar
/foo/bar ../../../../.. /
/x/y ./p/../q /x/q
/x/y /p/./q /p/q

Brief Summary / Idea (EN)
Use a stack: start from [] for absolute arg, or from split cwd for relative. Iterate components: skip empty & ., pop on .., push names otherwise. Join with /; if empty, return /. Time/space O(L).

The VOprep team has long accompanied candidates through various major company OAs and VOs, including OpenAI, Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for Stripe or similar engineering-focused companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.

END
 0