KatInterview

Canonical Path Cleanup Plus Overlapping Range Union

Greedy & IntervalsAuto-graded · 22 test cases
MediumAuto-gradedGreedy & Intervalsstacksortingintervals

Last updated

Combine stack-based Unix path normalization with sort-and-sweep merging of overlapping or touching closed ranges, returning both results in one list.

Canonical Path Cleanup Plus Overlapping Range Union

Write one function that solves two small independent tasks and returns both answers together.

Task A: Canonical path

You are given path, an absolute Unix-style path as a string. Produce its canonical form.

Interpret the path as a series of components separated by /:

  • An empty component (from repeated, leading, or trailing slashes) is ignored, so any run of slashes counts as one separator.
  • A component equal to . means "stay in the current directory" and is ignored.
  • A component equal to .. means "go up one level". If you are already at the root, it has no effect.
  • Any other component is a normal directory name and moves you one level down. This includes names such as ... or ..a, which are ordinary names and not special.

The canonical path must:

  • start with exactly one /,
  • separate directory names with exactly one /,
  • have no trailing / (except when the result is just the root /).

Task B: Merge ranges

You are given intervals, a list of closed ranges [start, end] with start <= end. The list is not necessarily sorted. Merge every group of ranges that overlap or touch (for example, [1, 3] and [3, 5] become [1, 5]) and return the resulting disjoint ranges sorted by start in ascending order.

If intervals is empty, the merged result is an empty list.

Output

Return a list of two elements: [canonical_path, merged_ranges], where canonical_path is a string and merged_ranges is a list of [start, end] lists.

Constraints

  • path is a non-empty string beginning with /.
  • 0 <= len(intervals) <= 10^4
  • Each interval has exactly two integers with start <= end.
  • Values may be negative or zero.
  • Touching ranges (end of one equals start of another) must be merged.

Examples

Example 1

Input: normalize_path_and_union_ranges('/a/./b/../../c/', [[1, 3], [2, 6], [8, 10], [15, 18]])

Output: ['/c', [[1, 6], [8, 10], [15, 18]]]

Example 2

Input: normalize_path_and_union_ranges('/home//foo/', [[1, 4], [4, 5]])

Output: ['/home/foo', [[1, 5]]]

Example 3

Input: normalize_path_and_union_ranges('/../', [[0, 0]])

Output: ['/', [[0, 0]]]

Example 4

Input: normalize_path_and_union_ranges('/a//b////c/d//././/..', [[5, 7], [1, 2], [2, 4], [10, 12], [11, 15]])

Output: ['/a/b/c', [[1, 4], [5, 7], [10, 15]]]

Hints

Hint 1

For the path, split on '/' and process the components from left to right while keeping a stack of directory names.

Hint 2

Decide what to do for each component: skip empty and '.', pop on '..' only if the stack is non-empty, and push everything else (even '...').

Hint 3

For the ranges, sort by start; then you only ever need to compare the next range with the last merged one. Merge when next start <= last end, and take the max of the ends.