The Unix find command allows you to search for files under a given directory. You can specify criteria for files you are interested in.
Imagine that you need to write code in a high-level language like Java that does things similar to the find command. I would like you to focus on two use cases at first:
- Find all files over
5 MBsomewhere under a directory. - Find all XML files somewhere under a directory.
I would like you to create a library that lets me do this easily. Keep in mind that these are just two use cases, and that the library should be flexible.
这道题考察的是如何设计一个可扩展的文件搜索库,核心思路不是把两个需求写死,而是抽象出统一的筛选接口,让路径遍历与条件判断解耦。可以用递归或栈遍历目录树,再配合组合式过滤器来表达“大小大于 5MB”“后缀是 XML”等规则;如果要支持更多条件,还可以把多个谓词按 AND/OR 组合,保持 API 灵活。面试时重点应说明如何定义文件元信息、如何组织搜索条件,以及如何让新增规则不修改已有逻辑。