- 0
- 0
- 约5.89千字
- 约 18页
- 2026-02-02 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年小米软件工程师面试流程及答案
一、编程能力测试(5题,共30分)
说明:考察基础编程能力、算法设计及代码质量。
1.编程题(6分):
题目:实现一个函数,输入一个整数数组,返回其中重复次数最多的元素及其重复次数。例如,输入`[1,2,2,3,3,3]`,输出`3,3`。
要求:
-时间复杂度O(n),空间复杂度O(1)。
-用Java或Python实现。
java
publicstaticStringfindMostFrequent(int[]nums){
//示例代码框架
MapInteger,IntegercountMap=newHashMap();
for(intnum:nums){
countMap.put(num,countMap.getOrDefault(num,0)+1);
}
intmaxCount=0;
intresult=0;
for(Map.EntryInteger,Integerentry:countMap.entrySet()){
if(entry.getValue()maxCount){
maxCount=entry.getValue();
result=entry.getKey();
}
}
returnresult+,+maxCount;
}
答案解析:
-使用哈希表统计每个数字的频率,遍历一次数组(O(n)时间),再遍历一次哈希表(O(k)时间,k为唯一数字数量,最坏为O(n))。
-空间复杂度为O(1)不成立,因为需要存储所有数字的频率,实际为O(n)。若题目允许哈希表,则此解合理;若要求O(1)空间,需其他方法(如计数排序,但需修改题目条件)。
2.编程题(8分):
题目:实现一个无重复字符的最长子串查找函数。例如,输入`abcabcbb`,输出`3`(对应子串`abc`)。
要求:
-使用滑动窗口法。
-用C++或Go实现。
cpp
intlengthOfLongestSubstring(strings){
intn=s.size();
intleft=0,right=0;
intmaxLen=0;
unordered_setcharwindow;
while(rightn){
if(window.find(s[right])==window.end()){
window.insert(s[right]);
maxLen=max(maxLen,right-left+1);
right++;
}else{
window.erase(s[left]);
left++;
}
}
returnmaxLen;
}
答案解析:
-滑动窗口法:左指针和右指针分别表示窗口的左右边界,通过哈希集合判断当前字符是否重复。
-时间复杂度O(n),每个字符最多被访问两次(右指针和左指针)。空间复杂度O(m),m为字符集大小(如ASCII为128)。
3.编程题(8分):
题目:实现一个简单的LRU(LeastRecentlyUsed)缓存,支持`get`和`put`操作。缓存容量为3。
要求:
-使用双向链表+哈希表实现。
-用JavaScript或C#实现。
javascript
classLRUCache{
constructor(capacity){
this.capacity=capacity;
this.map=newMap();
this.head=newNode(0,0);
this.tail=newNode(0,0);
this.head.next=this.tail;
this.tail.prev=this.head;
}
get(key){
if(!this.map.has(key))return-1;
letnode=this.map.get(key);
this.remove(node);
this.add(node);
returnnode.value;
}
put(key,value){
if(this.map.has(key)){
this.remove(this.map.get(key));
}
letnode=newNode(key,value);
this.map.set(key,node);
this.add(node);
if(this.map.sizethis.capacity){
letlru=this.ta
原创力文档

文档评论(0)