2026年数据面试题及抗压能力测试含答案.docxVIP

  • 0
  • 0
  • 约7.73千字
  • 约 19页
  • 2026-02-01 发布于福建
  • 举报

2026年数据面试题及抗压能力测试含答案.docx

第PAGE页共NUMPAGES页

2026年数据面试题及抗压能力测试含答案

一、编程题(共5题,每题20分,总分100分)

1.数据结构与算法:设计一个高效的算法,找出数组中第三大的数。

要求:不使用排序,时间复杂度尽可能低。

python

defthird_largest(nums):

first,second,third=float(-inf),float(-inf),float(-inf)

fornuminnums:

ifnumfirst:

third,second,first=second,first,num

eliffirstnumsecond:

third,second=second,num

elifsecondnumthird:

third=num

returnthirdifthird!=float(-inf)elseNone

2.数据结构与算法:实现一个LRU(最近最少使用)缓存,支持get和put操作。

要求:使用哈希表和双向链表实现,时间复杂度为O(1)。

python

classListNode:

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

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:

ifkeyinself.cache:

node=self.cache[key]

self._move_to_head(node)

returnnode.value

return-1

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

ifkeyinself.cache:

node=self.cache[key]

node.value=value

self._move_to_head(node)

else:

node=ListNode(key,value)

self.cache[key]=node

self._add_node(node)

iflen(self.cache)self.capacity:

node=self._pop_tail()

delself.cache[node.key]

def_move_to_head(self,node):

self._remove_node(node)

self._add_node(node)

def_add_node(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_pop_tail(self):

res=self.tail.prev

self._remove_node(res)

returnres

3.数据结构与算法:实现一个简单的Trie(前缀树),支持插入和查询操作。

要求:支持插入字符串和查询字符串是否存在。

python

classTrieNode:

def__init__(self):

self.children={}

self.is_end=False

classTrie:

def__init__(self):

self.root=TrieNode()

definsert(self,word:str)-None:

node=self.root

forcharinword:

ifcharnotinnode.children:

node.children[char]=TrieNode()

node=node.child

文档评论(0)

1亿VIP精品文档

相关文档