- 0
- 0
- 约7.23千字
- 约 22页
- 2026-02-10 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年软件开发工程师面试攻略与答案解析
一、编程基础(10题,每题10分,共100分)
注:考察数据结构、算法、编程语言基础,针对国内互联网及北美技术岗位。
1.题目:
请实现一个函数,输入一个字符串,返回该字符串中所有唯一字符的列表(不区分大小写)。例如,输入`HelloWorld`,输出`[H,e,W,r,d]`。
答案解析:
python
defunique_chars(s):
s=s.lower()
seen=set()
unique=[]
forcharins:
ifcharnotinseen:
seen.add(char)
unique.append(char)
returnunique
解析:
-使用`set`记录已出现字符,确保唯一性。
-转换为小写统一处理,避免大小写重复。
-时间复杂度O(n),空间复杂度O(n)。
2.题目:
给定一个数组`nums`和一个目标值`target`,请返回所有相加等于`target`的`nums`中两个数的索引(数组下标从0开始)。例如,`nums=[2,7,11,15]`,`target=9`,输出`[0,1]`。
答案解析:
python
deftwo_sum(nums,target):
num_dict={}
fori,numinenumerate(nums):
complement=target-num
ifcomplementinnum_dict:
return[num_dict[complement],i]
num_dict[num]=i
return[]
解析:
-使用哈希表`num_dict`存储数字及其索引,快速查找补数。
-时间复杂度O(n),空间复杂度O(n)。
3.题目:
请实现一个`LruCache`类,支持`get`和`put`操作,实现最近最少使用(LRU)缓存机制。
答案解析:
python
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)self.capacity:
self.cache.popitem(last=False)
解析:
-使用`OrderedDict`维护访问顺序,`move_to_end`更新最近使用。
-`put`时若超出容量,删除最久未使用项。
4.题目:
请实现一个函数,反转一个单链表。例如,输入`1-2-3-None`,输出`3-2-1-None`。
答案解析:
python
classListNode:
def__init__(self,val=0,next=None):
self.val=val
self.next=next
defreverse_list(head:ListNode)-ListNode:
prev=None
current=head
whilecurrent:
next_node=current.next
current.next=prev
prev=current
current=next_node
returnprev
解析:
-递归或迭代反转指针方向,注意`next_node`防止丢失链表。
5.题目:
请实现快速排序算法,对数组进行升序排序。
答案解析:
python
defquick_sort(arr):
iflen(arr)=1:
returnarr
pivot=arr[len(arr)//2]
left=[xforxinarrifxpivot]
middle=[xforxinarrifx==pivot]
right=[xforxinarrif
您可能关注的文档
最近下载
- 大学生国家安全知识竞赛考试题库500题(含答案).docx VIP
- 提高铝合金模板混凝土浇灌质量合格率.pdf VIP
- 江西省南昌市高考英语模拟试卷.docx VIP
- Lumerical-FDTD+Solutions中文指导手册.pdf
- 西工大与西安交大期末复习考研备考大学物理题库 六、波动光学 18 光的衍射.pdf VIP
- 模块8 特殊飞行《飞行原理》教学课件.pptx VIP
- 压疮的换药与护理.pptx VIP
- 计算机类本科毕业论文-基于微信小程序的在线刷题系统设计与实现.docx
- 个案护理一例不可分期的压疮.pptx VIP
- 2025年度多学科会诊中心工作总结及2026年工作安排.docx
原创力文档

文档评论(0)