← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Meta SWE coding round, one question about path resolution. Pretty straightforward on the surface but the relative vs absolute handling tripped me up more than I expected.

Questions Asked (1)

Q1

Given a current working directory path and a cd command path (which may be relative or absolute), write an algorithm to compute the resulting final path.

Algorithms & Data Structures
Author's notes

I jumped straight into the absolute path case because that felt easy, just return the cd path as-is.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Treat the problem as a stack-based simulation: split the cd path by '/' and process each component, handling '..' by popping from the stack and ignoring '.' and empty strings. If the cd path is absolute, start with an empty stack; otherwise, initialize the stack with the components of the current working directory. Finally, join the stack with '/' and prepend '/' to form the canonical absolute path.

Pro tip: Clarify edge cases upfront (e.g., paths with trailing slashes, multiple consecutive slashes, or '..' at root) and mention that the solution should run in O(n) time and O(n) space, where n is the length of the path. This shows attention to detail and efficiency.

1. Determine if the cd path is absolute or relative

Check if the cd path starts with '/'. If it does, it's absolute, so start with an empty stack. Otherwise, initialize the stack with the components of the current working directory.

2. Split the cd path into components

Split the cd path by '/' to get a list of components. This will include empty strings from consecutive slashes and '.' or '..' entries.

3. Process each component using a stack

Iterate through the components: if the component is empty or '.', skip it; if it's '..', pop from the stack if it's not empty; otherwise, push the component onto the stack.

4. Construct the final path

Join the stack elements with '/' and prepend a '/' to form the absolute path. If the stack is empty, the result is just '/'.

Key Points to Mention

  • Handling of '..' by popping from the stack, and ensuring it doesn't pop beyond the root.
  • Ignoring '.' and empty components (from consecutive slashes or trailing slashes).
  • Time and space complexity: O(n) time and O(n) space, where n is the length of the path.
  • Edge cases: root directory, paths with multiple slashes, paths ending with '..', and absolute vs relative paths.
  • Using a stack (or list) to simulate directory traversal.
  • Canonical path format: always starts with '/', no trailing slash except for root.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.