2026年滴出行技术面试题及答案详解.docxVIP

  • 0
  • 0
  • 约7.85千字
  • 约 19页
  • 2026-10-30 发布于福建
  • 举报

第PAGE页共NUMPAGES页

2026年滴出行技术面试题及答案详解

一、编程题(共3题,每题20分,总分60分)

题目1(20分):实现一个高效的LRU(最近最少使用)缓存机制

要求:

-使用Python语言实现。

-缓存容量固定,超出容量时需要淘汰最近最少使用的元素。

-提供get和put方法,分别用于获取和添加元素。

-时间复杂度要求为O(1)。

答案与解析:

python

classLRUCache:

def__init__(self,capacity:int):

self.capacity=capacity

self.cache={}

self.order=[]

defget(self,key:int)-int:

ifkeyinself.cache:

self.order.remove(key)

self.order.append(key)

returnself.cache[key]

return-1

defput(self,key:int,value:int)-None:

ifkeyinself.cache:

self.order.remove(key)

eliflen(self.cache)=self.capacity:

oldest_key=self.order.pop(0

文档评论(0)

1亿VIP精品文档

相关文档