2026年软件工程师面试指南与参考答案.docxVIP

  • 0
  • 0
  • 约6.75千字
  • 约 18页
  • 2026-01-13 发布于福建
  • 举报

2026年软件工程师面试指南与参考答案.docx

第PAGE页共NUMPAGES页

2026年软件工程师面试指南与参考答案

一、编程题(共3题,每题20分)

1.题目(20分):

编写一个函数,实现将任意长度的链表反转。要求不使用递归,时间复杂度为O(n),空间复杂度为O(1)。请用Python实现,并给出测试用例。

2.题目(20分):

给定一个包含重复元素的数组,请返回所有不重复的三元组,使得三元组内元素之和等于给定的目标值。要求时间复杂度为O(n2),空间复杂度为O(1)。请用Java实现,并给出测试用例。

3.题目(20分):

实现一个LRU(LeastRecentlyUsed)缓存机制,支持get和put操作。要求get操作返回对应键的值,若不存在则返回-1;put操作将键值对插入缓存,如果缓存已满则删除最久未使用的元素。请用JavaScript实现,并说明时间复杂度。

参考答案与解析

1.题目参考答案(Python):

python

classListNode:

def__init__(self,val=0,next=None):

self.val=val

self.next=next

defreverse_linked_list(head):

prev=None

current=head

whilecurrent:

next_node=current.next

current.next=prev

prev=current

current=next_node

returnprev

测试用例

node1=ListNode(1)

node2=ListNode(2)

node3=ListNode(3)

node1.next=node2

node2.next=node3

reversed_head=reverse_linked_list(node1)

print([reversed_head.val,reversed_head.next.val,reversed_head.next.next.val])#输出:[3,2,1]

解析:

-通过迭代方式反转链表,使用三个指针`prev`、`current`和`next_node`,逐步将链表节点反转。

-时间复杂度:O(n),只需遍历一次链表。

-空间复杂度:O(1),仅使用常数个额外空间。

2.题目参考答案(Java):

java

importjava.util.ArrayList;

importjava.util.Arrays;

importjava.util.List;

publicclassThreeSum{

publicListListIntegerthreeSum(int[]nums,inttarget){

Arrays.sort(nums);

ListListIntegerresult=newArrayList();

for(inti=0;inums.length-2;i++){

if(i0nums[i]==nums[i-1])continue;//跳过重复元素

intleft=i+1,right=nums.length-1;

while(leftright){

intsum=nums[i]+nums[left]+nums[right];

if(sum==target){

result.add(Arrays.asList(nums[i],nums[left],nums[right]));

while(leftrightnums[left]==nums[left+1])left++;

while(leftrightnums[right]==nums[right-1])right--;

left++;

right--;

}elseif(sumtarget){

left++;

}else{

right--;

}

}

}

returnresult;

}

//测试用例

publicstaticvoidmain(String[]args){

ThreeSumsolution=newThreeSum();

int[]nums={1,2,-2,-1,0,1};

inttarget=0;

System.out.println(solution.threeSum(nums,target));//输出:[[1,-1,0],[1,-2,1]]

}

}

解析:

-先对数组排

文档评论(0)

1亿VIP精品文档

相关文档