2026年IT行业软件工程师的面试题目集.docxVIP

  • 0
  • 0
  • 约6.56千字
  • 约 21页
  • 2026-01-19 发布于福建
  • 举报

2026年IT行业软件工程师的面试题目集.docx

第PAGE页共NUMPAGES页

2026年IT行业软件工程师的面试题目集

一、编程基础与数据结构(共5题,每题10分)

1.题目:

请实现一个函数,输入一个非负整数`n`,返回`n`的二进制表示中`1`的个数。例如,输入`11`(二进制为`1011`),返回`3`。

答案:

python

defcount_bits(n):

count=0

whilen:

count+=n1

n=1

returncount

解析:

利用位运算,每次右移一位并检查最低位是否为`1`,直到`n`为`0`。时间复杂度为O(logn)。

2.题目:

给定一个链表,判断是否为回文链表。例如,`1-2-2-1`是回文链表。

答案:

python

classListNode:

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

self.val=val

self.next=next

defis_palindrome(head):

ifnothead:

returnTrue

slow=fast=head

whilefastandfast.next:

slow=slow.next

fast=fast.next.next

stack=[]

whileslow:

stack.append(slow.val)

slow=slow.next

whilestack:

ifstack.pop()!=head.val:

returnFalse

head=head.next

returnTrue

解析:

快慢指针找到中点,后半部分入栈,然后与前半部分对比。时间复杂度O(n),空间复杂度O(n)。

3.题目:

实现一个LRU(最近最少使用)缓存,支持`get`和`put`操作。

答案:

python

classLRUCache:

def__init__(self,capacity:int):

self.capacity=capacity

self.cache={}

self.head,self.tail=Node(0,0),Node(0,0)

self.head.next=self.tail

self.tail.prev=self.head

defget(self,key:int)-int:

ifkeynotinself.cache:

return-1

node=self.cache[key]

self._remove(node)

self._add(node)

returnnode.value

defput(self,key:int,value:int)-None:

ifkeyinself.cache:

self._remove(self.cache[key])

node=Node(key,value)

self.cache[key]=node

self._add(node)

iflen(self.cache)self.capacity:

lru=self.tail.prev

self._remove(lru)

delself.cache[lru.key]

def_remove(self,node):

delself.cache[node.key]

node.prev.next=node.next

node.next.prev=node.prev

def_add(self,node):

node.next=self.head.next

node.next.prev=node

node.prev=self.head

self.head.next=node

解析:

使用双向链表+哈希表实现。链表维护访问顺序,哈希表实现O(1)访问。

4.题目:

给定一个数组,找出其中和为`target`的两个数,并返回它们的索引。例如,`nums=[2,7,11,15]`,`target=9`,返回`[0,1]`。

答案:

python

deftwo_sum(nums,target):

hash_map={}

fori,numinenumerate(nums):

complement=target-num

ifcomplementinhash_map:

return[hash_map[complement],i]

hash_map[num]=i

return[]

解析:

哈希表记录每个数的索引,时间复杂

文档评论(0)

1亿VIP精品文档

相关文档