互联网企业技术部门面试问题与答案.docxVIP

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

互联网企业技术部门面试问题与答案.docx

第PAGE页共NUMPAGES页

2026年互联网企业技术部门面试问题与答案

一、编程能力测试(5题,每题10分,共50分)

1.题目(10分):

请实现一个函数,输入一个字符串,返回该字符串中所有唯一字符的集合。例如,输入`abaccde`,输出`bde`。

答案与解析:

python

defunique_chars(s:str)-str:

char_set=set()

unique=set()

forcharins:

ifcharinchar_set:

unique.discard(char)

else:

char_set.add(char)

unique.add(char)

return.join(unique)

示例

print(unique_chars(abaccde))#输出:bde

解析:

-使用两个集合`char_set`和`unique`,前者记录遍历过的字符,后者记录唯一字符。

-遍历时,若字符已存在于`char_set`,则从`unique`中移除;否则加入`unique`。

-最终`unique`中即为唯一字符,转换为字符串返回。

-时间复杂度O(n),空间复杂度O(n)。

2.题目(10分):

给定一个链表,判断其是否为回文链表。例如,`1-2-2-1`是回文链表。

答案与解析:

python

classListNode:

def__init__(self,val=0,next=None):

self.val=val

self.next=next

defis_palindrome(head:ListNode)-bool:

ifnothead:

returnTrue

找到中点

slow=fast=head

whilefastandfast.next:

slow=slow.next

fast=fast.next.next

反转后半部分

prev=None

whileslow:

tmp=slow.next

slow.next=prev

prev=slow

slow=tmp

对比前后半部分

left,right=head,prev

whileright:#只需对比后半部分即可

ifleft.val!=right.val:

returnFalse

left=left.next

right=right.next

returnTrue

解析:

-使用快慢指针找到中点,慢指针到中点,快指针到末尾。

-反转后半部分链表,便于对比。

-对比前半部分和反转后的后半部分,若相同则为回文。

-时间复杂度O(n),空间复杂度O(1)。

3.题目(10分):

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

答案与解析:

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`存储键值对,列表`order`记录访问顺序。

-`get`操作:若键存在,则移到队尾表示最近使用;不存在返回-1。

-`put`操作:若键存在,则移到队尾;若超出容量,删除最久未使用的元素(队首)。

-时间复杂度O(1)。

4.题目(10分):

给定一个整数数组,返回所有和为`target`的三个数的组合。例如,`nums=[2,7,11,15]`,`target=9`,输出`[[2,7,0]]`。

答案与解析:

python

defthree_sum(nums:List[int],

文档评论(0)

1亿VIP精品文档

相关文档