赛富时工程师面试题及答案.docxVIP

  • 0
  • 0
  • 约6.25千字
  • 约 17页
  • 2026-01-30 发布于福建
  • 举报

第PAGE页共NUMPAGES页

2026年赛富时工程师面试题及答案

一、编程基础(5题,每题8分,共40分)

1.题目:

编写一个函数,实现字符串的翻转,不能使用现成的翻转函数(如Python中的`reverse()`)。

答案:

python

defreverse_string(s:str)-str:

returns[::-1]

解析:

使用Python的切片操作`[::-1]`可以实现字符串的逆序,时间复杂度为O(n),空间复杂度为O(n)。

2.题目:

给定一个整数数组,返回其中缺失的最小正整数。

答案:

python

deffind_missing_positive(nums:List[int])-int:

n=len(nums)

foriinrange(n):

while1=nums[i]=nandnums[nums[i]-1]!=nums[i]:

nums[nums[i]-1],nums[i]=nums[i],nums[nums[i]-1]

foriinrange(n):

ifnums[i]!=i+1:

returni+1

returnn+1

解析:

通过原地哈希的方式,将每个数放到其正确的位置(如`1`放在索引`0`处)。遍历数组后,第一个不满足`nums[i]==i+1`的位置对应的数字即为缺失的最小正整数。

3.题目:

实现一个LRU(最近最少使用)缓存,支持`get`和`put`操作。

答案:

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`记录键值对,`get`操作将键移动到末尾表示最近使用,`put`操作同样将键移动到末尾,并在超出容量时删除最早的键。

4.题目:

给定一个链表,判断是否为回文链表。

答案:

python

defis_palindrome(head:ListNode)-bool:

ifnotheadornothead.next:

returnTrue

slow=head

fast=head

whilefastandfast.next:

slow=slow.next

fast=fast.next.next

prev=None

whileslow:

temp=slow.next

slow.next=prev

prev=slow

slow=temp

left,right=head,prev

whileright:

ifleft.val!=right.val:

returnFalse

left=left.next

right=right.next

returnTrue

解析:

通过快慢指针找到链表中间,反转后半部分,然后逐个比较左右两部分的值是否相等。

5.题目:

实现二叉树的深度优先遍历(前序、中序、后序)。

答案:

python

前序遍历(递归)

defpreorder_recursive(root):

ifnotroot:

return[]

return[root.val]+preorder_recursive(root.left)+preorder_recursive(root.right)

中序遍历(递归)

definorder_recursive(root):

ifnotroot:

return[]

returninorder_recursive(root.left)+[root.val]+inorder_recursive(root.right)

后序遍历(递归)

defpostorder_recursive(root):

if

文档评论(0)

1亿VIP精品文档

相关文档