PracHub
QuestionsLearningGuidesInterview Prep
|Home/Coding & Algorithms/Meta

Resolve a cd Path Against the Current Directory

Last updated: Jul 22, 2026

Quick Overview

Implement simplified Unix-style cd path resolution for absolute and relative inputs containing repeated separators, dot components, and parent-directory steps. The challenge focuses on component semantics, root boundaries, empty arguments, exact normalization, and linear processing of long paths without filesystem lookups.

  • medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Resolve a cd Path Against the Current Directory

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Resolve a cd Path Against the Current Directory Implement path resolution for a simplified Unix-like `cd` command. `cwd` is a normalized absolute path. `argument` may be absolute or relative and may contain repeated `/` separators, `.` components, and `..` components. Resolving `..` at the root keeps the result at the root. Return a normalized absolute path with no trailing slash unless the result is `/`. Do not implement symbolic links, environment variables, `~`, wildcard expansion, or filesystem existence checks. ## Function Signature ```python def resolve_cd(cwd: str, argument: str) -> str: ... ``` ## Constraints - `1 <= len(cwd), len(argument) <= 200_000` - `cwd` starts with `/` and is already normalized. - Empty components created by repeated separators are ignored; every ordinary component contains non-slash characters. - An empty string argument leaves the current directory unchanged. Any argument beginning with `/` starts from the root, so `/` and `////` both resolve to `/`. ## Examples ```text Input: cwd = "/home/alex/projects", argument = "../docs/./api" Output: "/home/alex/docs/api" ``` ```text Input: cwd = "/a/b", argument = "/x//y/../../z" Output: "/z" ``` ```text Input: cwd = "/", argument = "../../../tmp" Output: "/tmp" ```

Quick Answer: Implement simplified Unix-style cd path resolution for absolute and relative inputs containing repeated separators, dot components, and parent-directory steps. The challenge focuses on component semantics, root boundaries, empty arguments, exact normalization, and linear processing of long paths without filesystem lookups.

Related Interview Questions

  • Choose the Cheapest Round Trip - Meta (medium)
  • Palindrome After Deleting at Most One Character - Meta (medium)
  • Validate Sorted Order Under a Custom Alphabet - Meta (medium)
  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
|Home/Coding & Algorithms/Meta

Resolve a cd Path Against the Current Directory

Meta logo
Meta
Mar 2, 2026, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

Resolve a cd Path Against the Current Directory

Implement path resolution for a simplified Unix-like cd command.

cwd is a normalized absolute path. argument may be absolute or relative and may contain repeated / separators, . components, and .. components. Resolving .. at the root keeps the result at the root. Return a normalized absolute path with no trailing slash unless the result is /.

Do not implement symbolic links, environment variables, ~, wildcard expansion, or filesystem existence checks.

Function Signature

def resolve_cd(cwd: str, argument: str) -> str:
    ...

Constraints

  • 1 <= len(cwd), len(argument) <= 200_000
  • cwd starts with / and is already normalized.
  • Empty components created by repeated separators are ignored; every ordinary component contains non-slash characters.
  • An empty string argument leaves the current directory unchanged. Any argument beginning with / starts from the root, so / and //// both resolve to / .

Examples

Input: cwd = "/home/alex/projects", argument = "../docs/./api"
Output: "/home/alex/docs/api"
Input: cwd = "/a/b", argument = "/x//y/../../z"
Output: "/z"
Input: cwd = "/", argument = "../../../tmp"
Output: "/tmp"

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Meta•More Software Engineer•Meta Software Engineer•Meta Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 9,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.