2026年Epic_Games工程师面试题及答案.docxVIP

  • 0
  • 0
  • 约5.77千字
  • 约 16页
  • 2026-01-29 发布于福建
  • 举报

第PAGE页共NUMPAGES页

2026年EpicGames工程师面试题及答案

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

考察方向:C++基础、算法、数据结构

1.题目:

编写一个C++函数,实现快速排序算法。输入一个整数数组,返回排序后的数组。要求使用递归方式实现,并解释时间复杂度和空间复杂度。

答案:

cpp

includevector

includeiostream

voidquickSort(std::vectorintarr,intleft,intright){

if(left=right)return;

intpivot=arr[(left+right)/2];

inti=left,j=right;

while(i=j){

while(arr[i]pivot)i++;

while(arr[j]pivot)j--;

if(i=j){

std::swap(arr[i],arr[j]);

i++;

j--;

}

}

if(leftj)quickSort(arr,left,j);

if(iright)quickSort(arr,i,right);

}

std::vectorintsortArray(std::vectorintnums){

quickSort(nums,0,nums.size()-1);

returnnums;

}

//示例用法

intmain(){

std::vectorintnums={3,6,8,10,1,2,1};

std::vectorintsorted=sortArray(nums);

for(intnum:sorted)std::coutnum;

return0;

}

解析:

快速排序的时间复杂度为O(nlogn),空间复杂度为O(logn),因为递归调用栈的深度为logn。最坏情况下(数组已排序或逆序),时间复杂度退化到O(n2)。

2.题目:

实现一个LRU(LeastRecentlyUsed)缓存,支持get和put操作。使用哈希表+双向链表实现,要求时间复杂度为O(1)。

答案:

cpp

includeunordered_map

includelist

classLRUCache{

private:

intcapacity;

std::listintcache;//双向链表,存储键值对

std::unordered_mapint,std::pairint,std::listint::iteratormap;//哈希表,存储键到值的映射和链表迭代器

public:

LRUCache(intcapacity_):capacity(capacity_){}

intget(intkey){

autoit=map.find(key);

if(it==map.end())return-1;

//将访问的元素移动到链表头部

cache.splice(cache.begin(),cache,it-second.second);

returnit-second.first;

}

voidput(intkey,intvalue){

autoit=map.find(key);

if(it!=map.end()){

//更新值,并将元素移动到链表头部

it-second.first=value;

cache.splice(cache.begin(),cache,it-second.second);

}else{

//如果缓存已满,删除链表尾部元素

if(cache.size()==capacity){

intoldKey=cache.back();

cache.pop_back();

map.erase(oldKey);

}

//新增元素到链表头部

cache.emplace_front(key,value);

map[key]={value,cache.begin()};

}

}

};

解析:

LRU缓存使用双向链表存储最近访问的元素,哈希表存储键到链表迭代器的映射,确保get和put操作的时间复杂度为O(1)。当访问或插入时,将元素移动到链表头部;当缓存满时,删除链表尾部元素。

3.题目:

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

答案:

cpp

includealgorithm

structTreeNode{

intval;

Tr

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档