阿里电话面试问题----100万个URL如何找到出现频率最高的前100个?

简介: 内推阿里电话面试中面试官给我出的一个题: 我想的头一个解决方案,就是放到stl 的map里面对出现的频率作为pair的第二个字段进行排序,之后按照排序结果返回: 下面口说无凭,show your code,当然在讨论帖子中遭遇了工程界大牛的sql代码在技术上的碾压。

内推阿里电话面试中面试官给我出的一个题:

我想的头一个解决方案,就是放到stl 的map里面对出现的频率作为pair的第二个字段进行排序,之后按照排序结果返回:

下面口说无凭,show your code,当然在讨论帖子中遭遇了工程界大牛的sql代码在技术上的碾压。什么是做工程的,什么是工程师的思维,不要一味的埋头搞算法。


讨论帖:

http://bbs.csdn.net/topics/391080906


python 抓取百度搜索结果的讨论贴:

http://bbs.csdn.net/topics/391077668


实验数据,python从百度抓得:

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""


import urllib2 
import re 
import os

#connect to a URL 
#一页的搜索结果中url大概是200个左右
file_url = open('url.txt','ab+')
#搜索框里的东西,这块可以设置成数字好让每次搜索的结果不一样
search = '123'
url = "http://www.baidu.com/s?wd="+search


def setUrlToFile():
    website = urllib2.urlopen(url) 
    #read html code 

    html = website.read() 

    #use re.findall to get all the links 

    links = re.findall('"((http|ftp)s?://.*?)"', html)
 

    for s in links:
        print s[0]
        if len(s[0]) < 256:
            file_url.write(s[0]+'\r\n')
    
#收集实验数据
for i in range(0,50):
    setUrlToFile()

file_url.close()


###需要重新打开再读一下
file_url = open('url.txt','r')
file_lines = len(file_url.readlines())
print "there are %d url in %s" %(file_lines,file_url)
file_url.close()

方法1:

c++  写的读 url.txt放到map里面

对map<string , int>的value进行排序,得到前100个

运行一下也就55s,还是很快的,url长度进行了限制小于256个字符


#pragma once
/*
//计算代码段运行时间的类
//
*/
#include <iostream>

#ifndef ComputeTime_h
#define ComputeTime_h


//单位毫秒

class   ComputeTime    
{  
private:  
	int Initialized;  
	__int64 Frequency;  
	__int64 BeginTime;  
		    
public:  

	bool Avaliable();  
	double End();  
	bool Begin();  
	ComputeTime();  
	virtual   ~ComputeTime();    

};  






#endif
#include "stdafx.h"
#include "ComputeTime.h"
#include <iostream>
#include <Windows.h>

ComputeTime::ComputeTime()  
{  
	Initialized=QueryPerformanceFrequency((LARGE_INTEGER   *)&Frequency);  
}  
   
 ComputeTime::~ComputeTime()  
{  
		    
}  
   
 bool   ComputeTime::Begin()  
{  
	if(!Initialized)  
		return 0;

	 return   QueryPerformanceCounter((LARGE_INTEGER   *)&BeginTime);  
 }
     
 double   ComputeTime::End()
{  
	 if(!Initialized)  
		return 0;

		   
	 __int64   endtime;  
		   
	 QueryPerformanceCounter((LARGE_INTEGER   *)&endtime);  
		    
		  
	 __int64   elapsed = endtime-BeginTime;  
		    
		  
	 return   ((double)elapsed/(double)Frequency)*1000.0;  //单位毫秒
 }  

 bool   ComputeTime::Avaliable()
{  
	 return Initialized;  
}   


// sortUrl.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
//#include <utility>    
#include <vector>
#include <map>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include "ComputeTime.h"

using namespace std;

map<string,int> urlfrequency;


typedef pair<string, int> PAIR;


struct CmpByValue 
{
	bool operator()(const PAIR& lhs, const PAIR& rhs) 
	{
		return lhs.second > rhs.second;
	}
};

void find_largeTH(map<string,int> urlfrequency)
{
	//把map中元素转存到vector中 ,按照value排序
	vector<PAIR> url_quency_vec(urlfrequency.begin(), urlfrequency.end());
	sort(url_quency_vec.begin(), url_quency_vec.end(), CmpByValue());
	//url_quency_vec.size()
	for (int i = 0; i != 100; ++i) 
	{
		cout<<url_quency_vec[i].first<<endl;
		cout<<url_quency_vec[i].second<<endl;
	}
}


//urlheap的建立过程,URL插入时候存在的
void insertUrl(string url)
{
	pair<map<string ,int>::iterator, bool> Insert_Pair;
	Insert_Pair = urlfrequency.insert(map<string, int>::value_type(url,1));



	if (Insert_Pair.second == false)
	{
		(Insert_Pair.first->second++);
	}
	

}


int _tmain(int argc, _TCHAR* argv[])
{
	fstream URLfile;
	char buffer[1024]; 
	URLfile.open("url.txt",ios::in|ios::out|ios::binary);

	if (! URLfile.is_open())  
	{ cout << "Error opening file"; exit (1); } 
	else
	{
	cout<<"open file success!"<<endl;
	}

	ComputeTime cp;
	cp.Begin();
	int i = 0;
	 while (!URLfile.eof())  
	{  
	URLfile.getline (buffer,1024);  
	//cout << buffer << endl;  
	string temp(buffer);
	//cout<<i++<<endl;
	insertUrl(temp);
	}  
	      


	find_largeTH(urlfrequency);

	cout<<"running time: "<<cp.End()<<"ms"<<endl;

	getchar();
	//system("pause");
	return 0;
}



实验结果:55s还不算太差,可以接受,毕竟是头脑中的第一个解决方案。



方法2:

hash code 版本,只是不知道怎么 hash和url关联起来:


// urlFind.cpp : 定义控制台应用程序的入口点。
//

// sortUrl.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
 
#include <vector>
#include <map>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_map>
#include "ComputeTime.h"

using namespace std;

map<unsigned int,int> urlhash;


typedef pair<unsigned int, int> PAIR;


struct info{
	string url;
	int cnt;
	bool operator<(const info &r) const {
		return cnt>r.cnt;
	}
};


unordered_map<string,int> count;

//priority_queue<info> pq;


struct CmpByValue 
{
	bool operator()(const PAIR& lhs, const PAIR& rhs) 
	{
		return lhs.second > rhs.second;
	}
};

void find_largeTH(map<unsigned int,int> urlhash)
{
	//把map中元素转存到vector中 ,按照value排序
	vector<PAIR> url_quency_vec(urlhash.begin(), urlhash.end());
	sort(url_quency_vec.begin(), url_quency_vec.end(), CmpByValue());
	//url_quency_vec.size()
	for (int i = 0; i != 100; ++i) 
	{
		cout<<url_quency_vec[i].first<<endl;
		cout<<url_quency_vec[i].second<<endl;
	}
}


// BKDR Hash Function
unsigned int BKDRHash(char *str)
{
	unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
	unsigned int hash = 0;

	while (*str)
	{
		hash = hash * seed + (*str++);
	}

	return (hash & 0x7FFFFFFF);
}

//
void insertUrl(string url)
{

	unsigned int hashvalue = BKDRHash((char *)url.c_str());
	pair<map<unsigned int ,int>::iterator, bool> Insert_Pair;
	Insert_Pair = urlhash.insert(map<unsigned int, int>::value_type(hashvalue,1));

	if (Insert_Pair.second == false)
	{
		(Insert_Pair.first->second++);
	}


}


int _tmain(int argc, _TCHAR* argv[])
{
	fstream URLfile;
	char buffer[1024]; 
	URLfile.open("url.txt",ios::in|ios::out|ios::binary);

	if (! URLfile.is_open())  
	{ cout << "Error opening file"; exit (1); } 
	else
	{
		cout<<"open file success!"<<endl;
	}

	ComputeTime cp;
	cp.Begin();
	int i = 0;
	while (!URLfile.eof())  
	{  
		URLfile.getline (buffer,1024);  
		//cout << buffer << endl;  
		string temp(buffer);
		//cout<<i++<<endl;
		insertUrl(temp);
	}  



	find_largeTH(urlhash);

	cout<<"running time: "<<cp.End()<<"ms"<<endl;

	getchar();
	//system("pause");
	return 0;
}





性能15秒左右:缺点在于没有把hashcode和url进行关联,技术的处理速度已经非常可观了


方法3:

下面用STL的hash容器unordered_map,和优先队列(就是堆)来实现这个问题。


// urlFind.cpp : 定义控制台应用程序的入口点。
//

// sortUrl.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
 
#include <vector>
#include <map>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_map>
#include <queue>
#include "ComputeTime.h"

using namespace std;


typedef pair<string, int> PAIR;


struct info
{
	string url;
	int cnt;
	bool operator<(const info &r) const
	{
		return cnt<r.cnt;
	}
};


unordered_map<string,int> hash_url;

priority_queue<info> pq;



void find_largeTH(unordered_map<string,int> urlhash)
{

	unordered_map<string,int>::iterator iter = urlhash.begin();
	info temp;
	for (; iter!= urlhash.end();++iter)
	{
		temp.url = iter->first;
		temp.cnt = iter->second;
		pq.push(temp);
	}

	for (int i = 0; i != 100; ++i) 
	{

		cout<<pq.top().url<<endl;
		cout<<pq.top().cnt<<endl;
		pq.pop();
	}
}



void insertUrl(string url)
{

	pair<unordered_map<string ,int>::iterator, bool> Insert_Pair;
	Insert_Pair = hash_url.insert(unordered_map<string, int>::value_type(url,1));

	if (Insert_Pair.second == false)
	{
		(Insert_Pair.first->second++);
	}

}

int _tmain(int argc, _TCHAR* argv[])
{
	fstream URLfile;
	char buffer[1024]; 
	URLfile.open("url.txt",ios::in|ios::out|ios::binary);

	if (! URLfile.is_open())  
	{ cout << "Error opening file"; exit (1); } 
	else
	{
		cout<<"open file success!"<<endl;
	}

	ComputeTime cp;
	cp.Begin();
	int i = 0;
	while (!URLfile.eof())  
	{  
		URLfile.getline (buffer,1024);  
		//cout << buffer << endl;  
		string temp(buffer);
		//cout<<i++<<endl;
		insertUrl(temp);
	}  

	find_largeTH(hash_url);

	cout<<"running time: "<<cp.End()<<"ms"<<endl;

	getchar();
	//system("pause");
	return 0;
}


基本上算是算法里面比较优秀的解决方案了,面试官如果能听到这个方案应该会比较欣喜。





方法4:实验耗时未知,技术上碾压了上述解决方案,中高年轻人,不要重复造轮子!哈哈

数据库,SQL语句:

load data infile "d:/bigdata.txt" into table tb_url(url);

SELECT
	url,
	count(url) as show_count
	FROM
	tb_url
	GROUP BY url
	ORDER BY show_count desc
	LIMIT 100




相关文章
|
18天前
|
存储 SQL 算法
阿里面试:每天新增100w订单,如何的分库分表?这份答案让我当场拿了offer
例如,在一个有 10 个节点的系统中,增加一个新节点,只会影响到该新节点在哈希环上相邻的部分数据,其他大部分数据仍然可以保持在原节点,大大减少了数据迁移的工作量和对系统的影响。狠狠卷,实现 “offer自由” 很容易的, 前段时间一个武汉的跟着尼恩卷了2年的小伙伴, 在极度严寒/痛苦被裁的环境下, offer拿到手软, 实现真正的 “offer自由”。在 3 - 5 年的中期阶段,随着业务的稳定发展和市场份额的进一步扩大,订单数据的增长速度可能会有所放缓,但仍然会保持在每年 20% - 30% 的水平。
阿里面试:每天新增100w订单,如何的分库分表?这份答案让我当场拿了offer
|
2月前
|
监控 Kubernetes Java
阿里面试:5000qps访问一个500ms的接口,如何设计线程池的核心线程数、最大线程数? 需要多少台机器?
本文由40岁老架构师尼恩撰写,针对一线互联网企业的高频面试题“如何确定系统的最佳线程数”进行系统化梳理。文章详细介绍了线程池设计的三个核心步骤:理论预估、压测验证和监控调整,并结合实际案例(5000qps、500ms响应时间、4核8G机器)给出具体参数设置建议。此外,还提供了《尼恩Java面试宝典PDF》等资源,帮助读者提升技术能力,顺利通过大厂面试。关注【技术自由圈】公众号,回复“领电子书”获取更多学习资料。
|
1月前
|
缓存 NoSQL Java
阿里面试:DDD 落地,遇到哪些 “拦路虎”?如何破局?
为每个子领域定义限界上下文(bounded context),限界上下文是一个清晰定义了领域模型的边界的范围。在限界上下文内,领域模型的概念是一致的,但不同限界上下文之间可以有不同的模型和语言。界限上下文,基本可以对应到 落地层面的 微服务。这就是 DDD 建模和 微服务架构, 能够成为孪生兄弟、 天然统一的原因。具体的方法论和落地实操,请参考 《第34章视频 DDD学习圣经》DDD 战略设计的第一步就是统一语言,也叫通用语言(UBIQUITOUS LANGUAGE),用于定义上下文。
阿里面试:DDD 落地,遇到哪些 “拦路虎”?如何破局?
|
5月前
|
存储 关系型数据库 MySQL
阿里面试:为什么要索引?什么是MySQL索引?底层结构是什么?
尼恩是一位资深架构师,他在自己的读者交流群中分享了关于MySQL索引的重要知识点。索引是帮助MySQL高效获取数据的数据结构,主要作用包括显著提升查询速度、降低磁盘I/O次数、优化排序与分组操作以及提升复杂查询的性能。MySQL支持多种索引类型,如主键索引、唯一索引、普通索引、全文索引和空间数据索引。索引的底层数据结构主要是B+树,它能够有效支持范围查询和顺序遍历,同时保持高效的插入、删除和查找性能。尼恩还强调了索引的优缺点,并提供了多个面试题及其解答,帮助读者在面试中脱颖而出。相关资料可在公众号【技术自由圈】获取。
|
26天前
|
算法 NoSQL 应用服务中间件
阿里面试:10WQPS高并发,怎么限流?这份答案让我当场拿了offer
在 Nacos 的配置管理界面或通过 Nacos 的 API,创建一个名为(与配置文件中 dataId 一致)的配置项,用于存储 Sentinel 的流量控制规则。上述规则表示对名为的资源进行流量控制,QPS 阈值为 10。resource:要保护的资源名称。limitApp:来源应用,default表示所有应用。grade:限流阈值类型,1 表示 QPS 限流,0 表示线程数限流。count:限流阈值。strategy:流控模式,0 为直接模式,1 为关联模式,2 为链路模式。
阿里面试:10WQPS高并发,怎么限流?这份答案让我当场拿了offer
|
2月前
|
人工智能 缓存 Ubuntu
AI+树莓派=阿里P8技术专家。模拟面试、学技术真的太香了 | 手把手教学
本课程由阿里P8技术专家分享,介绍如何使用树莓派和阿里云服务构建AI面试助手。通过模拟面试场景,讲解了Java中`==`与`equals`的区别,并演示了从硬件搭建、语音识别、AI Agent配置到代码实现的完整流程。项目利用树莓派作为核心,结合阿里云的实时语音识别、AI Agent和文字转语音服务,实现了一个能够回答面试问题的智能玩偶。课程展示了AI应用的简易构建过程,适合初学者学习和实践。
127 22
|
2月前
|
人工智能 Java
产品经理-面试问题(高频率)
本文全面介绍初入产品岗位的基本面试问题,涵盖离职原因、技术沟通、薪资期望、到岗时间、个人优劣势及竞品调研分析等内容。针对每个问题提供详细回答示例,帮助求职者更好地准备面试,提升应答技巧和自信心。内容涉及职业成长、公司文化匹配、工作与生活平衡等多方面考量,助力求职者找到理想职位。
|
3月前
|
存储 NoSQL 架构师
阿里面试:聊聊 CAP 定理?哪些中间件是AP?为什么?
本文深入探讨了分布式系统中的“不可能三角”——CAP定理,即一致性(C)、可用性(A)和分区容错性(P)三者无法兼得。通过实例分析了不同场景下如何权衡CAP,并介绍了几种典型分布式中间件的CAP策略,强调了理解CAP定理对于架构设计的重要性。
166 4
|
4月前
|
存储 NoSQL 算法
阿里面试:亿级 redis 排行榜,如何设计?
本文由40岁老架构师尼恩撰写,针对近期读者在一线互联网企业面试中遇到的高频面试题进行系统化梳理,如使用ZSET排序统计、亿级用户排行榜设计等。文章详细介绍了Redis的四大统计(基数统计、二值统计、排序统计、聚合统计)原理和应用场景,重点讲解了Redis有序集合(Sorted Set)的使用方法和命令,以及如何设计社交点赞系统和游戏玩家排行榜。此外,还探讨了超高并发下Redis热key分治原理、亿级用户排行榜的范围分片设计、Redis Cluster集群持久化方式等内容。文章最后提供了大量面试真题和解决方案,帮助读者提升技术实力,顺利通过面试。
|
4月前
|
SQL 关系型数据库 MySQL
阿里面试:1000万级大表, 如何 加索引?
45岁老架构师尼恩在其读者交流群中分享了如何在生产环境中给大表加索引的方法。文章详细介绍了两种索引构建方式:在线模式(Online DDL)和离线模式(Offline DDL),并深入探讨了 MySQL 5.6.7 之前的“影子策略”和 pt-online-schema-change 方案,以及 MySQL 5.6.7 之后的内部 Online DDL 特性。通过这些方法,可以有效地减少 DDL 操作对业务的影响,确保数据的一致性和完整性。尼恩还提供了大量面试题和解决方案,帮助读者在面试中充分展示技术实力。

热门文章

最新文章