主要问网络方面的
- Socket连接过程
- TCP Handshake
- TCP/UDP
- Threads versus Processes
- Website Access Control:数据吞吐量过大,数据保存时间不长,不能用数据库实现,用Hash Table在内存中运行
- 预估全球所有计算机访问(IPv4)9 Byte的一个数据结构(ip, time, count)最大占用内存:40GB
- time: UNIX时间从1970年1月1日开始的秒数
Website Access Control
实现功能:某特定IP在10秒钟内只能访问2次。
- (1, 0) => true
- (1, 1) => true
- (1, 2) => false
- (1, 10) => true
在牛客网(Nowcoder)上面试的,面试过程有点紧张,写完之后改了大概七八分钟才运行起来,还好面试官中途有事出去了一趟,我整理了一下代码和思路。
之前一个其他公司面试拿到一个前端offer是一个Coding Challenge,3天完成一个小项目即可,跟这次的感觉还是有点不一样的。
#include <iostream>
#include <map>
using namespace std;
struct User{
int ip;
int time;
int count;
User(int ip, int time, int count)
:
ip(ip),
time(time),
count(0)
{}
};
map<int, User> m;
const int max_time = 10;
bool check(int ip, int time)
{
auto user = m.find(ip);
if(user == m.end()) {
User* u = new User(ip, time, 1);
m.insert(make_pair(ip, *u));
return true;
}
if(time - user->second.time >= max_time) {
user->second.time = time;
user->second.count = 1;
return true;
}
user->second.count++;
if(user->second.count >= 2) return false;
return true;
}
int main() {
cout << check(1, 0) << endl;
// cout << m.begin()->first << m.begin()->second.count;
cout << check(1, 1) << endl;
cout << check(1, 2) << endl;
//cout << "\n1,2: " << m.begin()->first << m.begin()->second.count;
cout << check(1, 10) << endl;
}
Related Topics
Berkely Sockets

