京东物流高级工程师面试技巧及题目.docxVIP

  • 0
  • 0
  • 约5.87千字
  • 约 15页
  • 2026-02-05 发布于福建
  • 举报

京东物流高级工程师面试技巧及题目.docx

第PAGE页共NUMPAGES页

2026年京东物流高级工程师面试技巧及题目

一、编程能力(5题,共30分)

1.(6分)编写一个函数,实现快速排序算法,并分析其时间复杂度和空间复杂度。

答案与解析:

python

defquick_sort(arr):

iflen(arr)=1:

returnarr

pivot=arr[len(arr)//2]

left=[xforxinarrifxpivot]

middle=[xforxinarrifx==pivot]

right=[xforxinarrifxpivot]

returnquick_sort(left)+middle+quick_sort(right)

时间复杂度:O(nlogn),平均情况;最坏情况O(n^2)

空间复杂度:O(logn),递归栈空间

解析:快速排序通过分治思想实现,核心是选择枢轴(pivot)并进行分区。时间复杂度受枢轴选择影响,空间复杂度主要由递归栈决定。

2.(6分)实现一个LRU(最近最少使用)缓存,要求支持get和put操作,并说明实现思路。

答案与解析:

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)

解析:LRU缓存通过双向链表和哈希表实现。链表维护访问顺序,哈希表提供O(1)查找。get操作将元素移至队尾,put操作需淘汰最久未使用元素(队首)。

3.(8分)编写一个函数,统计一个字符串中所有字符的出现频率,并返回一个字典。

答案与解析:

python

fromcollectionsimportdefaultdict

defcount_frequency(s:str)-dict:

freq=defaultdict(int)

forcharins:

freq[char]+=1

returndict(freq)

解析:使用`defaultdict`简化计数过程,遍历字符串统计每个字符的频次。时间复杂度O(n),空间复杂度O(m),m为字符集大小。

4.(5分)实现一个二叉树的中序遍历,要求使用递归和非递归两种方式。

答案与解析:

python

递归方式

definorder_traversal_recursive(root):

ifnotroot:

return[]

returninorder_traversal_recursive(root.left)+[root.val]+inorder_traversal_recursive(root.right)

非递归方式(栈)

definorder_traversal_iterative(root):

stack,result=[],[]

node=root

whilestackornode:

whilenode:

stack.append(node)

node=node.left

node=stack.pop()

result.append(node.val)

node=node.right

returnresult

解析:递归方式直观但栈溢出风险高;非递归方式用栈模拟递归,避免栈溢出。时间复杂度均为O(n),空间复杂度O(h),h为树高。

5.(5分)编写一个函数,判断一个链表是否存在环,并返回环的入口节点(若有)。

答案与解析:

python

defdetect_cycle(head):

slow,fast=head,head

whilefastandfast.next:

slow=slo

文档评论(0)

1亿VIP精品文档

相关文档