Resolve a cd Command Inside a Sandboxed Code Interpreter
Last updated: November 29, 2026
Quick Overview
Given the current working directory and the argument to a `cd` command (absolute or relative, possibly containing `.`, `..` and repeated slashes), return the resulting normalized absolute path.
OpenAI
November 29, 20260
0
0 solved
Given the current working directory and the argument to a `cd` command (absolute or relative, possibly containing `.`, `..` and repeated slashes), return the resulting normalized absolute path.
OpenAI's code execution sandbox (the environment behind ChatGPT's data analysis tool) has to track a virtual working directory for every session, so a path-resolution warm-up is a natural phone screen opener. The interviewer checks that you handle relative versus absolute targets, never climb above root, and write clean code quickly enough to leave room for follow-ups like symlinks.
What the Interviewer Expects
- Distinguish absolute targets from relative ones before processing
- Treat empty segments and '.' as no-ops
- Pop on '..' but never above root
- Return a canonical path with a single leading slash and no trailing slash
- Talk through tests including root and trailing slashes
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you support `~` for the home directory?
- Symlinks: given a map from path to target, how does resolution change and how do you detect a symlink loop?
- How would you support `cd -` to return to the previous directory?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Inputs:
cwd, an absolute normalized path such as/home/user, andtarget, the argument tocd. - If
targetstarts with/it is absolute andcwdis ignored. .means stay,..means p...
High-Level Design
Model the path as a stack of directory names. Seed the stack with the components of cwd (or start empty for an absolute target), then walk the components of target left to right. Each name pushes,...