互联网公司技术总监面试题及解答.docxVIP

  • 1
  • 0
  • 约6.96千字
  • 约 20页
  • 2026-04-03 发布于福建
  • 举报

第PAGE页共NUMPAGES页

2026年互联网公司技术总监面试题及解答

一、编程与算法题(共5题,每题10分,总分50分)

1.题目:

实现一个LRU(LeastRecentlyUsed)缓存机制,支持get和put操作。缓存容量为固定值,当缓存满时,需要淘汰最久未使用的元素。请用Python或Java实现,并说明时间复杂度和空间复杂度。

答案:

Python实现:

python

fromcollectionsimportOrderedDict

classLRUCache:

def__init__(self,capacity:int):

self.capacity=capacity

self.cache=OrderedDict()

defget(self,key:int)-int:

ifkeynotinself.cache:

return-1

self.cache.move_to_end(key)

returnself.cache[key]

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

ifkeyinself.cache:

self.cache.move_to_end(key)

self.cache[key]=value

iflen(self.cache)s

文档评论(0)

1亿VIP精品文档

相关文档