← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Meta coding screen, path manipulation problem that looks easy until you actually sit down and think about edge cases. Walked away feeling okay about it but not great.

Questions Asked (1)

Q1

Given two absolute directory paths, return the shortest relative path string that would navigate from the first to the second, as if passed to the `cd` command.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I started by splitting both paths on '/' and finding the common prefix, which is the right instinct.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Split both paths into components, find the longest common prefix, then construct the relative path by going up (..) for each remaining component in the source and down into the remaining components of the target. Handle edge cases like identical paths, root directory, and trailing slashes. Explain the algorithm clearly and discuss time/space complexity.

Pro tip: Mention that this is essentially a string manipulation problem but requires careful handling of path semantics (e.g., absolute paths always start with '/', and '..' from root stays at root). Also, consider if the target is a subdirectory of the source, the relative path is just the remaining components without any '..'.

1. Clarify and Normalize Inputs

Confirm that both paths are absolute and may contain trailing slashes. Normalize by removing trailing slashes (except for root) and splitting into components.

2. Find Longest Common Prefix

Iterate through both component lists simultaneously to find the longest prefix of directories they share. This represents the common ancestor directory.

3. Construct Relative Path

For each remaining component in the source path after the common prefix, add '..'. Then append the remaining components of the target path. Join with '/'.

4. Handle Edge Cases

If the paths are identical, return '.' or an empty string (depending on convention). If the target is the root, the relative path is just the necessary '..'s. If the source is the root, the relative path is the target path without leading slash.

5. Analyze Complexity and Test

State that time complexity is O(n) where n is the total number of components, and space is O(n) for storing components. Walk through a few examples to verify correctness.

Key Points to Mention

  • Path normalization: handling trailing slashes and multiple consecutive slashes.
  • Longest common prefix approach for finding the common ancestor directory.
  • Using '..' to go up from source to common ancestor, then down to target.
  • Edge cases: identical paths, root directory, target is subdirectory of source.
  • Time and space complexity: O(n) time, O(n) space where n is number of path components.
  • Potential pitfalls: assuming paths are normalized, not handling '..' in input (though absolute paths typically don't have them).

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