🎁 Exclusive Offer! Join our
Telegram Channel
to get **special discounts** and updates! 🚀
Question 84 - 100% Working Solution | Buy Now
Description
12 Live
Directory Structure (SDE Question) - Part A
You are given a directory structure represented as a tree, where each node represents a directory that can have any number of child directories.
- Each child directory will have a unique name.
- The second line of the input will contain the root name and its child directories.
Based on the given input, you have to construct the directory structure and implement three functions: countDescendants, cutPaste, and copyPaste.
Function Details:
1. countDescendants(path)
- Takes one argument: directory path
- Returns the number of descendants of the directory
Examples:
countDescendants("root") => 7
countDescendants("root/a") => 2
countDescendants("root/b") => 0
countDescendants("root/c") => 2
countDescendants("root/a/d") => 0
countDescendants("root/a/e") => 0
2. cutPaste(src, dest)
- It takes two arguments: source directory and destination directory
- Cuts the source directory from its parent and pastes it inside the destination directory
Example:
cutPaste("root/a", "root/c")
3. copyPaste(src, dest)
- It takes two arguments: source directory and destination directory
- Copies the source directory and pastes it into the destination
Example:
copyPaste("root/a","root/c")
Output:
- countDescendants(node): print the total number of descendants or "Invalid command"
- cutPaste(src, dest): print "OK" or "Invalid command"
- copyPaste(src, dest): print "OK" or "Invalid command"
Negative Conditions:
- If the source is an ancestor of the destination, print "Invalid command"
- If source = destination, print "Invalid command"
- If the destination already has a directory of the source's name, print "Invalid command"
- If not a valid path for the source or destination, print "Invalid command"
- If command causes total nodes > 10^6, print "Invalid command"
Input Bounds:
- 1 <= n <= 10^5 (structure lines)
- 1 <= q <= 10^5 (commands)
- Total number of nodes at any point: <= 10^6