软件开发工程师高级软件开发工程师面试题及答案.docxVIP

  • 1
  • 0
  • 约7.62千字
  • 约 24页
  • 2026-02-09 发布于福建
  • 举报

软件开发工程师高级软件开发工程师面试题及答案.docx

第PAGE页共NUMPAGES页

2026年软件开发工程师高级软件开发工程师面试题及答案

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

1.题目(10分):

请用Python实现一个函数,输入一个正整数n,返回其阶乘值。要求:不能使用内置的`math.factorial`函数,需考虑大数计算(如n=100),并优化时间复杂度。

答案与解析:

python

deffactorial(n):

ifn==0:

return1

result=1

foriinrange(1,n+1):

result=i

returnresult

解析:

-朴素解法:直接循环相乘,时间复杂度为O(n),但若n过大(如100),Python整数会自动转为长整型,但效率较低。

-优化方案:可考虑分治法(递归拆分)或内存池优化(减少内存分配),但题目要求简洁,故采用基础实现。若需进一步优化,可结合多线程分块计算。

2.题目(10分):

给定一个字符串`s`,包含数字和字母,请实现函数返回其中所有唯一数字字符(不重复)的子串数量。例如,`s=a1b2c1`返回3(a1b,1bc,2c)。

答案与解析:

python

defunique_digit_substrings(s):

count=0

seen=set()

foriinrange(len(s)):

ifs[i].isdigit()ands[i]notinseen:

count+=1

seen.add(s[i])

returncount

解析:

-核心思路:遍历时记录每个数字字符是否已出现过,若未出现则表示可形成新的唯一子串。

-时间复杂度:O(n),空间复杂度:O(1)(假设数字字符有限)。

3.题目(10分):

实现一个LRU(最近最少使用)缓存,支持`get(key)`和`put(key,value)`操作。要求:使用哈希表+双向链表实现,时间复杂度均为O(1)。

答案与解析:

python

classListNode:

def__init__(self,key=0,value=0):

self.key=key

self.value=value

self.prev=None

self.next=None

classLRUCache:

def__init__(self,capacity:int):

self.capacity=capacity

self.cache={}

self.head,self.tail=ListNode(),ListNode()

self.head.next=self.tail

self.tail.prev=self.head

defget(self,key:int)-int:

ifkeynotinself.cache:

return-1

node=self.cache[key]

self._move_to_front(node)

returnnode.value

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

ifkeyinself.cache:

node=self.cache[key]

node.value=value

self._move_to_front(node)

else:

node=ListNode(key,value)

self.cache[key]=node

self._add_to_front(node)

iflen(self.cache)self.capacity:

self._remove_lru()

def_move_to_front(self,node):

self._remove_node(node)

self._add_to_front(node)

def_add_to_front(self,node):

node.prev=self.head

node.next=self.head.next

self.head.next.prev=node

self.head.next=node

def_remove_node(self,node):

prev_node=node.prev

next_node=node.next

prev_node.next=next_node

next_node.prev=prev_node

def_remove_lru(self):

lru=self.tail.prev

self._remove_node(lru)

文档评论(0)

1亿VIP精品文档

相关文档