- 2
- 0
- 约9.45千字
- 约 27页
- 2026-01-30 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年程序员高级面试指南及考核要点
一、编程语言与数据结构(15分,共5题)
1.题目(3分):
编写一段Java代码,实现一个函数`findMaxProduct`,输入一个整数数组`nums`,返回数组中三个数的最大乘积。假设数组长度至少为3,且所有元素均为32位整数。
答案:
java
publicintfindMaxProduct(int[]nums){
Arrays.sort(nums);
intn=nums.length;
//情况1:三个最大的正数相乘
longproduct1=(long)nums[n-1]nums[n-2]nums[n-3];
//情况2:两个最小的负数与最大的正数相乘
longproduct2=(long)nums[0]nums[1]nums[n-1];
return(int)Math.max(product1,product2);
}
解析:
-排序后,最大乘积可能来自两种情况:①三个最大正数相乘;②两个最小负数(绝对值大)与最大正数相乘。
-时间复杂度:O(nlogn)(排序主导),可优化至O(n)通过遍历找到最大三个数和最小两个数。
2.题目(3分):
用Python实现一个函数`topKFrequent`,输入一个非空数组`words`和一个整数`k`,返回出现频率最高的`k`个单词列表,按频率降序排列。若频率相同,按单词字母顺序升序排列。
答案:
python
fromcollectionsimportCounter
deftopKFrequent(words,k):
count=Counter(words)
按频率降序,字母升序排序
sorted_words=sorted(count.items(),key=lambdax:(-x[1],x[0]))
return[wordforword,freqinsorted_words[:k]]
解析:
-使用`Counter`统计词频,然后按频率降序、字母升序排序。
-Python内置排序稳定,`sorted`会保留相同频率的相对顺序。
3.题目(3分):
设计一个数据结构`LRUCache`(最近最少使用缓存),支持`get`和`put`操作。容量为`capacity`,超出容量时删除最久未使用的元素。
答案:
python
classNode:
def__init__(self,key,value):
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=Node(0,0)
self.tail=Node(0,0)
self.head.next=self.tail
self.tail.prev=self.head
defget(self,key:int)-int:
ifkeyinself.cache:
node=self.cache[key]
self._move_to_front(node)
returnnode.value
return-1
defput(self,key:int,value:int)-None:
ifkeyinself.cache:
node=self.cache[key]
node.value=value
self._move_to_front(node)
else:
iflen(self.cache)==self.capacity:
self._remove_lru()
new_node=Node(key,value)
self.cache[key]=new_node
self._add_to_front(new_node)
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(s
原创力文档

文档评论(0)