Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Linux Path Simplification

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Linux Path Simplification. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Meta's Linux-based production hosts receive paths from command-line tooling that may contain redundant separators and directory references. Implement a function that returns the canonical form of an absolute Linux path.

Resolve the path using these rules:

  1. Consecutive / characters act as one separator.
  2. A . component means the current directory and should be removed.
  3. A .. component moves to the parent directory. If the path is already at /, remain at /.
  4. Any other component is a directory name and should be preserved.
  5. The result must begin with exactly one / and must not end with /, unless the result is the root path /.

Formal Specification

Given a string path containing an absolute Linux path, return a string containing its canonical absolute path. Directory names do not contain /; names such as ... or file.. are ordinary directory names.

Constraints

  • 1 <= len(path) <= 10^4
  • path begins with /
  • Components contain printable non-slash characters
  • The path may contain repeated /, ., and .. components

Function Signature

def simplify_path(path):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output