1. lazy array
// lazy array,要求实现arraymap(…).indexOf(…),其中map传进去一个function,
//indexOf返回运行了所有function之后传入值的index。要求map的操作最后再做,所以叫 lazy array。 For example:
// arr = LazyArray([10,20, 30,40, 50])
/arrmap(lambdaxx*2)indexOf(40)---->1
//arrmap(lambda xx*2)map(lambdaxx*3)indexOf(240)->3注意这里重新开了一个chain,上一行的map就不计算在内了
After finishing the implementation, asked about how to make class unit test friendly And write unit test for the class.
Then ask how to improved the performance of lazyArray. My answer is using cache to store chain and correponding calculated value.
//indexOf返回运行了所有function之后传入值的index。要求map的操作最后再做,所以叫 lazy array。 For example:
// arr = LazyArray([10,20, 30,40, 50])
/arrmap(lambdaxx*2)indexOf(40)---->1
//arrmap(lambda xx*2)map(lambdaxx*3)indexOf(240)->3注意这里重新开了一个chain,上一行的map就不计算在内了
After finishing the implementation, asked about how to make class unit test friendly And write unit test for the class.
Then ask how to improved the performance of lazyArray. My answer is using cache to store chain and correponding calculated value.
2. Design WebCrawler
First write single threaded Web Crawler, Then write multi-threaded WebCrawler
/开始提供了三个utilityfunction:
/(1)fetch(urL:url)->HTML:content:
//(2)parse(HTML:content)->List<URL>;
//(3)save(URL:url,HTML:content)。
//save是把数据存在disk上,"fetch是发个network request,
//parse是in-memory解析html page,
/**
*improve the efficiency by multi-thread
* keys
*1-avoid race condition where 2 threads check same url
*and try to process as next url at same time (this causes duplicate visit on same url)
*use concurrentHashMap put to avoid, since the put (insert entry) lock the segment
of map
* and if return null meaning no such key in map previously which means we can process the url
*2- save is a disk 1/0 where we should put it into a separate thread pool to let it finish byitself
*3- fetch html is a networki/0
*/
3. check obstacles
input:
二维整型数组operations,每一个元素有两种可能,对应两种不同的操作:
1) [1,x]:在坐标(无限延伸)的x位置放一个obstacle
2)[2,l,x]:check当前[x-l,x]上有无obstacle,若有则在结果字符串上append1,否则
在结果字符串上append 0
Return:
字符串res,res.charAt(i)对应在operations中出现的第i个check操作的结果
example:
input :operations=[[1,2],[2,2,3],[1,5],[2,8,1]]
output:”01”
explaination:
a)[1,2]:在×=2上放一个obstacle
b)[2,2,3]在内存在[3-2,3]一个obstacle(x=2),所以往res中append0
C)[1,5]:在x=5上放一个obstacle
d)[2,8,1]因为在[7,8]区间范围内不存在obstacle所以我那个res中append1
d128在内不存在bstacle,所以我那个res中append1
4. 设计total revenue系统
写一个算total revenue的系统,给两个参数,一个list of string priceList,另一个list of
string log
priceList例子:["itemname:priceNumber","cup:10”]
log例子:有点复杂具体忘了,但是全是字符串。大意有三种字符串:1.卖什么物品卖几件
2.打折开始什么物品减价多少maxcount(卖几件后恢复原价)3.打折结束
string log
priceList例子:["itemname:priceNumber","cup:10”]
log例子:有点复杂具体忘了,但是全是字符串。大意有三种字符串:1.卖什么物品卖几件
2.打折开始什么物品减价多少maxcount(卖几件后恢复原价)3.打折结束
5. 求figure掉下来之后的board状态
给一个二维度矩阵board,该board中的元素可以为三种char,分别为:1."."代表空格2.’F’代表figure3.’#’代表障碍。其中fiqure是连为一体的,并且会往下掉;当figure遇到障碍或bottom时,整体会停下来。求figure掉下来之后的board状态,返回board。
按要求最小的move steps然后让F进行相应的移动。注意每一列如何找最小的move steps(从上往下每一个#和它之前离他最近的F的差再-1,这些diff求最小值)
按要求最小的move steps然后让F进行相应的移动。注意每一列如何找最小的move steps(从上往下每一个#和它之前离他最近的F的差再-1,这些diff求最小值)