← 返回 google 的题目列表Implement deleteDirectory with getChildrenPaths and deleteFile APIs
类型:online_judge
You work with a filesystem abstraction that provides:
List<String> getChildrenPaths(String path): returns all direct child paths under path (each child may be a file or a directory). Returns an empty list for an empty directory.
void deleteFile(String path): deletes a file at path (assume it’s a file; deleting a directory may error).
Implement:
void deleteDirectory(String path): delete the directory at path and everything under it (all nested subdirectories and files).
Requirements:
You must delete all contents under the directory before deleting the directory itself (if applicable; the post doesn’t specify a directory-delete API, so state your assumption).
Discuss trade-offs among implementations, such as:
recursive DFS vs iterative DFS with an explicit stack
whether parallel deletion is worth it (rate limiting, failures, partial deletes)
memory usage and maximum depth
Assume valid paths and no cycles. If you address errors (e.g., getChildrenPaths/deleteFile failures), clearly state the policy (retry/skip/fail-fast).
Provide pseudocode or code in a language of your choice.
Example
Input
(API based; no stdin)
Directory tree: /a -> [/a/f1, /a/b]; /a/b -> [/a/b/f2]
Output
deleteFile(/a/f1), deleteFile(/a/b/f2), (delete /a/b), (delete /a)