2026年软件工程师招聘面试题与解析.docxVIP

  • 4
  • 0
  • 约5.37千字
  • 约 15页
  • 2026-04-03 发布于福建
  • 举报

第PAGE页共NUMPAGES页

2026年软件工程师招聘面试题与解析

一、编程实现题(共5题,每题10分,总分50分)

(针对互联网行业,侧重算法与数据结构,考察实际编码能力)

1.题目:

实现一个LRU(LeastRecentlyUsed)缓存,支持`get`和`put`操作。缓存容量为固定值`capacity`,当访问或插入元素导致缓存容量超出时,需要淘汰最久未使用的元素。请用Python实现,并说明时间复杂度。

答案与解析:

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.capacit

文档评论(0)

1亿VIP精品文档

相关文档