Google 面试经验分享:餐厅等位列表数据结构设计题 - Oavoservice
2025-05-15

Google 0515 面经
Candidate 在 Google coding 环节遇到了设计题 —— 餐厅等位列表数据结构。
今天面试官是一个资深工程师,看起来比较专业,先是双方自我介绍,然后直接就是 Coding,上来就出一道经典的数据结构设计题。
题目描述
实现一个餐厅等位列表的数据结构,支持以下操作:
- 顾客排队加入
- 已排队顾客离开
- 当有空桌时,根据桌子大小安排最先到达且人数不超过桌子大小的顾客入座
解题思路
这是一道经典的数据结构设计问题,需要合理选择数据结构来高效支持各种操作:
- 顾客队列:使用优先队列,按到达时间排序
- 快速查找:使用哈希表存储顾客信息,支持O(1)删除
- 桌子分配:遍历队列找到第一个满足条件的顾客
代码实现
import heapq
from collections import defaultdict
class Customer:
def __init__(self, customer_id, party_size, arrival_time):
self.customer_id = customer_id
self.party_size = party_size
self.arrival_time = arrival_time
def __lt__(self, other):
return self.arrival_time < other.arrival_time
class RestaurantQueue:
def __init__(self):
# 优先队列存储等待的顾客,按到达时间排序
self.waiting_queue = []
# 哈希表存储顾客信息,支持快速删除
self.customer_map = {}
# 全局时间戳
self.timestamp = 0
def add_customer(self, customer_id, party_size):
"""顾客排队加入"""
self.timestamp += 1
customer = Customer(customer_id, party_size, self.timestamp)
heapq.heappush(self.waiting_queue, customer)
self.customer_map[customer_id] = customer
print(f"顾客 {customer_id} (人数: {party_size}) 已加入队列")
def remove_customer(self, customer_id):
"""已排队顾客离开"""
if customer_id in self.customer_map:
customer = self.customer_map[customer_id]
# 标记为已删除,实际删除在分配桌子时进行
customer.customer_id = None
del self.customer_map[customer_id]
print(f"顾客 {customer_id} 已离开队列")
else:
print(f"顾客 {customer_id} 不在队列中")
def assign_table(self, table_size):
"""根据桌子大小安排顾客入座"""
# 清理已删除的顾客
while self.waiting_queue and self.waiting_queue[0].customer_id is None:
heapq.heappop(self.waiting_queue)
# 找到第一个满足条件的顾客
temp_queue = []
assigned_customer = None
while self.waiting_queue:
customer = heapq.heappop(self.waiting_queue)
if customer.customer_id is not None and customer.party_size <= table_size:
assigned_customer = customer
break
elif customer.customer_id is not None:
temp_queue.append(customer)
# 将未分配的顾客重新加入队列
for customer in temp_queue:
heapq.heappush(self.waiting_queue, customer)
if assigned_customer:
del self.customer_map[assigned_customer.customer_id]
print(f"桌子大小 {table_size} 已分配给顾客 {assigned_customer.customer_id} (人数: {assigned_customer.party_size})")
return assigned_customer
else:
print(f"没有适合桌子大小 {table_size} 的顾客")
return None
def get_queue_status(self):
"""获取当前队列状态"""
active_customers = [c for c in self.waiting_queue if c.customer_id is not None]
return [(c.customer_id, c.party_size, c.arrival_time) for c in active_customers]
算法分析
- 加入队列:O(log n) - 优先队列插入
- 离开队列:O(1) - 哈希表删除
- 分配桌子:O(n log n) - 最坏情况下需要遍历整个队列
- 空间复杂度:O(n) - 存储所有顾客信息
测试用例
# 测试代码
restaurant = RestaurantQueue()
# 顾客加入队列
restaurant.add_customer("A", 2) # 2人
restaurant.add_customer("B", 4) # 4人
restaurant.add_customer("C", 1) # 1人
restaurant.add_customer("D", 3) # 3人
# 查看队列状态
print("当前队列:", restaurant.get_queue_status())
# 分配桌子
restaurant.assign_table(2) # 2人桌
restaurant.assign_table(4) # 4人桌
# 顾客离开
restaurant.remove_customer("B")
# 再次分配
restaurant.assign_table(3) # 3人桌
面试官看完直接点头:"Excellent design! The data structure choices are optimal."
Candidate 成功拿下这一轮。
这就是 OAVOSERVICE 实时 VO 辅助 的威力:
不是死记硬背题库,而是帮你在最关键的时刻,快速理清思路,稳定发挥,稳稳过关!
面试技巧分享
在 Google 的面试中,除了算法实现,面试官还会关注:
1. 数据结构选择
能够合理选择数据结构(优先队列、哈希表)来高效支持各种操作。
2. 时间复杂度分析
清楚分析每个操作的时间复杂度,并能够优化瓶颈操作。
3. 边界条件处理
考虑各种边界情况,如空队列、顾客不存在、桌子大小不匹配等。
4. 代码可读性
写出清晰、模块化的代码,注意类设计和接口设计。
Oavoservice 实时辅助服务
在这次 Google 面试中,我们的实时辅助系统发挥了关键作用:
- 快速思路提示:在候选人卡壳时,立即提供数据结构设计的核心思路
- 算法指导:推荐使用优先队列和哈希表来实现高效操作
- 代码优化:提供最优的代码实现,确保时间和空间复杂度
- 设计思维:帮助候选人从系统设计角度思考问题
结语
餐厅等位列表数据结构设计虽然是一道经典题目,但在面试的紧张环境下,能够快速理清思路并正确实现并不容易。这就是为什么我们的实时辅助服务如此重要。
如果你正在准备 Google 或其他公司的技术面试,欢迎联系 Oavoservice。我们拥有丰富的面试经验和专业的辅助团队,能够在你最需要的时候提供及时有效的帮助。
Oavoservice - 让每一次面试都成为成功的机会!
需要辅助的同学随时dd哦,vo开始前支持mock!