Google VO 面试复盘|这道 File System 题,真正靠的是一次完整的现场辅助拉正思路 – 一亩三分地 – 谷歌

这是一次 Google 的 VO 技术面试。

面试题英文原文(完整)

You are given a file system represented as a map from entityId to entity.

Each entity has:

  • an entityId
  • a name
  • a type: either file or directory

If the entity is a file, it has a fixed size.
If the entity is a directory, it contains a list of child entity IDs.

Given an entity ID, return the total size of that entity.

  • If it is a file, return its size.
  • If it is a directory, return the sum of sizes of all files under it recursively.

面试官随后给了一个完整示例,用来确认候选人是否真的理解了这个 file system 是如何被建模的。

root (id = 1)
├── dir (id = 2)
│ ├── file1 (id = 4): 100b
│ └── file2 (id = 5): 200b
└── file3 (id = 3): 300b

{
  1: { type: 'directory', name: 'root', children: [2, 3] },
  2: { type: 'directory', name: 'dir', children: [4, 5] },
  4: { type: 'file', name: 'file1', size: 100 },
  5: { type: 'file', name: 'file2', size: 200 },
  3: { type: 'file', name: 'file3', size: 300 }
}

澄清阶段

候选人一开始没有急着写代码,而是跟着我们辅助老师的思路先确认了一件事:
entityId 是否可以假设为全局唯一,并且是在 entity 创建时就确定的。

这个问题本身没有任何花活,但问得非常稳,面试官也直接点头确认。
到这里为止,一切都很正常。这也给后面拼命拼命写码争取了时间


候选人开始设计整体结构的时候,很自然地把注意力放在了 directory 的 size 怎么递归计算上。
这个方向本身没错,但在面试压力下,他已经开始下意识地把**所有 entity 都当成“可能有 children”**的节点来处理。

当讨论到示例里的 id = 4 时,他明显已经在往“继续往下找 children”这个方向想了。

这一步在现场非常真实,也非常常见。
不是不会,而是脑子已经开始自动套“树 + DFS”的模板了。


后续的面试是一整段完整的思路拉正

首先明确一件事:
id = 4 对应的是一个 FileEntity,它根本没有 children。

其次,整个设计的关键不是“你在哪里写递归”,而是 getSize 的职责归属

我们当时直接帮他把结构拆清楚了:

如果是 file,它的 getSize 就只返回自己的 size。
如果是 directory,它的 getSize 才去遍历 children,然后对每个 child 再调用 getSize。

FileSystem 这一层不做任何类型判断,它只负责一件事:
通过 map 拿到 entity,然后调用 entity 自己的 getSize。

这里依赖的是 polymorphism,不是 if / else。

这一整套逻辑,是在现场完整给出来的,而不是让候选人自己慢慢试错。


候选人顺着辅助思路,逻辑才真正跑顺

在这个结构被明确之后,候选人明显轻松了很多。

他不再纠结“这个 id 有没有 children”,
也不再试图在 FileSystem 里处理 file 和 directory 的区别。

逻辑一下子变得非常干净:
file 就是 file,directory 就是 directory,各自对自己的 size 负责。

到这里,这道题才算真正进入了 Google 想看到的轨道。


如果没有这次完整辅助,会发生什么

如果没有在这个节点把结构一次性辅助,候选人大概率会继续在外层写条件分支,
一边算 size,一边打补丁。

这种代码不一定当场写不出来,但在 Google 面试里,后续的 follow-up 基本都会直接卡死。

如果你也在准备Apple、Meta、TikTok等大厂的算法与系统设计面试,却不清楚如何拆题和应对各种边界,欢迎添加微信 csvohelp,即可领取北美面试求职通关秘诀。我们也有代面试,面试辅助,OA代写等服务助您早日上岸~

Leave a Reply

Your email address will not be published. Required fields are marked *