- 0
- 0
- 约7.74千字
- 约 22页
- 2026-01-20 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年IT企业招聘官如何面试软件工程师问题集
一、编程能力测试(共5题,每题10分)
1.题目:
编写一个函数,实现快速排序算法。输入一个无序数组,输出排序后的数组。要求说明时间复杂度和空间复杂度。
答案与解析:
python
defquick_sort(arr):
iflen(arr)=1:
returnarr
pivot=arr[len(arr)//2]
left=[xforxinarrifxpivot]
middle=[xforxinarrifx==pivot]
right=[xforxinarrifxpivot]
returnquick_sort(left)+middle+quick_sort(right)
解析:
快速排序的时间复杂度为O(nlogn),空间复杂度为O(logn)。该算法采用分治策略,通过选择基准值(pivot)将数组分为三部分,递归排序左右子数组。
2.题目:
实现一个LRU(LeastRecentlyUsed)缓存,支持get和put操作。要求使用Python或Java实现,并说明时间复杂度。
答案与解析:
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)
delself.cache[oldest_key]
self.cache[key]=value
self.order.append(key)
解析:
LRU缓存通过双向链表和哈希表实现,get操作的时间复杂度为O(1),put操作的时间复杂度为O(1)。链表维护访问顺序,哈希表实现快速查找。
3.题目:
编写一个函数,实现二叉树的深度优先遍历(前序、中序、后序)。要求使用递归或迭代方式实现。
答案与解析:
python
classTreeNode:
def__init__(self,val=0,left=None,right=None):
self.val=val
self.left=left
self.right=right
defpreorder_traversal(root):
ifnotroot:
return[]
result=[]
stack=[root]
whilestack:
node=stack.pop()
result.append(node.val)
ifnode.right:
stack.append(node.right)
ifnode.left:
stack.append(node.left)
returnresult
definorder_traversal(root):
result=[]
stack=[]
current=root
whilestackorcurrent:
whilecurrent:
stack.append(current)
current=current.left
current=stack.pop()
result.append(current.val)
current=current.right
returnresult
defpostorder_traversal(root):
ifnotroot:
return[]
result=[]
stack=[(root,False)]
whilestack:
node,visited=stack.pop()
ifnode:
ifvisited:
result.append(node.val)
else:
stack.append((node,True))
stack.append((node.right,False))
您可能关注的文档
最近下载
- GB 7594.1-1987 电线电缆橡皮绝缘和橡皮护套 第1部分一般规定-国家标准.pdf VIP
- 110kV送变电工程启动调试与试运行操作指南及案例解析.docx VIP
- 心衰合并肾功能不全的护理难点与解决方案.pptx VIP
- 关于2024年度民主生活会整改措施落实情况及2025年深入贯彻中央八项规定精神学习教育查摆问题整改情况的通报.docx VIP
- 2024-2025学年广东省潮州市高二上学期期末教学质量检测物理试卷.pdf VIP
- 上肢动脉CTA扫描技术课件最新完整版本.pptx VIP
- 110kV变电站专项电气试验及调试方案.doc VIP
- 2024年湖南汽车工程职业学院单招职业技能测试题库及答案(历年真题).docx VIP
- 广东省潮州市2024-2025学年高三上学期期末教学质量检测物理试卷.docx VIP
- 上海电力学院大一机械制图C习题本解答(造福学弟,不谢)-新版.pptx
原创力文档

文档评论(0)