京东物流工程师面试题集与解析.docxVIP

  • 0
  • 0
  • 约7.89千字
  • 约 22页
  • 2026-03-09 发布于福建
  • 举报

第PAGE页共NUMPAGES页

2026年京东物流工程师面试题集与解析

一、编程基础(共5题,每题10分,总分50分)

1.题目:

编写一个函数,实现快速排序算法,并分析其时间复杂度。要求输入一个整数数组,返回排序后的数组。

答案与解析:

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(n2)(最坏情况,当输入数组已经有序或接近有序时)。其核心思想是选择一个基准值(pivot),将数组分为小于、等于和大于基准值的三部分,然后递归地对左右两部分进行排序。京东物流的业务场景中,排序算法常用于订单处理、路径优化等场景,快速排序因其高效性被广泛应用。

2.题目:

实现一个LRU(LeastRecentlyUsed)缓存,支持get和put操作,要求用哈希表和双向链表结合的方式实现。

答案与解析:

python

classListNode:

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

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=ListNode()

self.tail=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_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=ListNode(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_remove_node(self,node):

node.prev.next=node.next

node.next.prev=node.prev

def_add_to_front(self,node):

node.prev=self.head

node.next=self.head.next

self.head.next.prev=node

self.head.next=node

示例用法

cache=LRUCache(2)

cache.put(1,1)

cache.put(2,2)

print(cache.get(1))#返回1

cache.put(3,3)#去除键2

print(cache.get(2))#返回-1

解析:

LRU缓存通过双向链表和哈希表结合的方式实现,双向链表维护元素的访问顺序,哈希表实现O(1)的get和put操作。当访问某个元素时,将其移动到链表头部;当插入新元素时,若缓存已满,则删除链表尾部元素(即最近最少使用元素)。京东物流的仓储管理系统、订单查询场景中,LRU缓存可用于优化热点数据访问。

3.题目:

给定一个二叉树,判断其是否是平衡二叉树(左右子树高度差不超过1)。

答案与解析:

python

cl

文档评论(0)

1亿VIP精品文档

相关文档