- 0
- 0
- 约8.04千字
- 约 24页
- 2026-02-11 发布于福建
- 举报
第PAGE页共NUMPAGES页
2026年工程师面试技巧及面试题解析
一、编程语言与基础算法(10题,共50分)
1.题目1(5分):
编写一段Python代码,实现一个函数`count_vowels(s)`,输入一个字符串`s`,返回其中元音字母(a,e,i,o,u)的数量(不区分大小写)。
示例:`count_vowels(HelloWorld)`返回`3`。
答案:
python
defcount_vowels(s):
vowels=aeiou
returnsum(1forcharins.lower()ifcharinvowels)
解析:
使用`lower()`统一转为小写,遍历字符串统计元音字母。时间复杂度O(n),空间复杂度O(1)。
2.题目2(5分):
用C++实现快速排序算法(QuickSort),要求选择第一个元素作为基准(pivot)。
答案:
cpp
includevector
usingnamespacestd;
intpartition(vectorintarr,intleft,intright){
intpivot=arr[left];
intl=left+1,r=right;
while(l=r){
if(arr[l]pivotarr[r]pivot)swap(arr[l++],arr[r--]);
if(arr[l]=pivot)l++;
if(arr[r]=pivot)r--;
}
swap(arr[left],arr[r]);
returnr;
}
voidquick_sort(vectorintarr,intleft,intright){
if(left=right)return;
intpivot_idx=partition(arr,left,right);
quick_sort(arr,left,pivot_idx-1);
quick_sort(arr,pivot_idx+1,right);
}
解析:
快速排序核心是分治思想,通过`partition`函数将数组划分为三部分(小于、等于、大于基准的元素)。平均时间复杂度O(nlogn),最坏O(n2)。
3.题目3(10分):
假设你正在开发一个电商系统,用户需要根据价格区间(如`[100,200]`)筛选商品。请设计一个二叉搜索树(BST)实现,要求支持:
(1)插入商品节点(包含价格、ID);
(2)查询价格在区间`[low,high]`的所有节点;
(3)返回结果数量。
答案:
python
classTreeNode:
def__init__(self,price,id):
self.price=price
self.id=id
self.left=None
self.right=None
classBST:
def__init__(self):
self.root=None
definsert(self,price,id):
self.root=self._insert(self.root,price,id)
def_insert(self,node,price,id):
ifnotnode:
returnTreeNode(price,id)
ifpricenode.price:
node.left=self._insert(node.left,price,id)
elifpricenode.price:
node.right=self._insert(node.right,price,id)
returnnode
defsearch_range(self,low,high):
result=[]
self._search_range(self.root,low,high,result)
returnresult
def_search_range(self,node,low,high,result):
ifnotnode:
return
iflownode.price:
self._search_range(node.left,low,high,result)
iflow=node.price=high:
result.append((node.price,node.id))
ifhighnode.price:
self._search_range(node.right,l
原创力文档

文档评论(0)