2026年IT行业软件开发工程师面试技巧与答案.docxVIP

  • 0
  • 0
  • 约8.2千字
  • 约 20页
  • 2026-01-20 发布于福建
  • 举报

2026年IT行业软件开发工程师面试技巧与答案.docx

第PAGE页共NUMPAGES页

2026年IT行业软件开发工程师面试技巧与答案

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

1.题目(10分):

编写一个函数,实现快速排序算法,并分析其时间复杂度。假设输入数组为`[4,1,3,9,7]`,请输出排序后的结果。

答案:

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)

arr=[4,1,3,9,7]

sorted_arr=quick_sort(arr)

print(sorted_arr)#输出:[1,3,4,7,9]

解析:

快速排序通过分治法实现排序,时间复杂度为平均O(nlogn),最坏情况O(n2)。上述代码选择中间值作为基准,递归分割数组。实际面试中可能要求手写递归逻辑或优化分区方式(如三数取中法)。

2.题目(10分):

给定一个字符串`s=abracadabra`,请编写代码找到其中不重复的最长子串,并输出长度。例如,acdr是一个不重复的子串,长度为4。

答案:

python

deflength_of_longest_substring(s):

char_map={}

left=0

max_len=0

forrightinrange(len(s)):

ifs[right]inchar_mapandchar_map[s[right]]=left:

left=char_map[s[right]]+1

char_map[s[right]]=right

max_len=max(max_len,right-left+1)

returnmax_len

s=abracadabra

print(length_of_longest_substring(s))#输出:5(arcad)

解析:

滑动窗口技术,通过双指针遍历字符串,哈希表记录字符上一次出现的位置。时间复杂度O(n),空间复杂度O(min(m,n)),其中m为字符集大小。

3.题目(10分):

实现一个LRU(最近最少使用)缓存,支持`get`和`put`操作。假设容量为3,初始缓存为`{1:1,2:2}`,执行`put(3,3)`后,再执行`get(2)`,返回值应为`2`。

答案:

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=self.order.pop(0)

delself.cache[oldest]

self.cache[key]=value

self.order.append(key)

示例

cache=LRUCache(3)

cache.put(1,1)

cache.put(2,2)

cache.put(3,3)#缓存:{1:1,2:2,3:3}

print(cache.get(2))#输出:2

解析:

使用哈希表存储键值对,双向链表维护使用顺序。`get`操作将访问的键移到链表末尾,`put`操作先删除最久未使用的键(如果超出容量),然后添加新键值对。时间复杂度O(1)。

4.题目(10分):

给定一个链表`1-2-3-4-5`,反转该链表,并输出反转后的链表。例如,`5-4-3-2-

文档评论(0)

1亿VIP精品文档

相关文档