Amazon 面试准备

终结者T800
• 阅读 6009

https://instant.1point3acres....
1.Product of Array Except Self
2.给两天的用户log数据,求两天均登陆Amazon的用户,follow up - log很大不可以放入内存
相关问题,统计访问量最高的前10个ip,内存不够怎么办。统计最高的单词,内存不够。

(大数据处理,当内存不够的时候,考虑取模后映射成小文件,然后再用hash分别统计,然后再堆排序)

Time is O(n*log(k)).

class Pair{
    int num;
    int count;
    public Pair(int num, int count){
        this.num=num;
        this.count=count;
    }
}
 
public class Solution {
    public List<Integer> topKFrequent(int[] nums, int k) {
        //count the frequency for each element
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int num: nums){
            if(map.containsKey(num)){
                map.put(num, map.get(num)+1);
            }else{
                map.put(num, 1);
            }
        }
 
        // create a min heap
        PriorityQueue<Pair> queue = new PriorityQueue<Pair>(new Comparator<Pair>(){
            public int compare(Pair a, Pair b){
                return a.count-b.count;
            }
        });
 
        //maintain a heap of size k. 
        for(Map.Entry<Integer, Integer> entry: map.entrySet()){
            Pair p = new Pair(entry.getKey(), entry.getValue());
            queue.offer(p);
            if(queue.size()>k){
                queue.poll();
            }
        }
 
        //get all elements from the heap
        List<Integer> result = new ArrayList<Integer>();
        while(queue.size()>0){
            result.add(queue.poll().num);
        }
        //reverse the order
        Collections.reverse(result);
 
        return result;
    }
}

3.HashMap 构造, 如何解决collision.

HashMap works on the principle of hashing, we have put() and get() method for storing and retrieving object from HashMap.When we pass both key and value to put() method to store on HashMap, it uses key object hashcode() method to calculate hashcode and them by applying hashing on that hashcode it identifies bucket location for storing value object. While retrieving it uses key object equals method to find out correct key value pair and return value object associated with that key. HashMap uses linked list in case of collision and object will be stored in next node of linked list. Also, HashMap stores both key and value tuple in every node of linked list in the form of Map.Entry object.

What will happen if two different objects have the same hashcode?
Since hashcode is same, bucket location would be same and collision will occur in HashMap Since HashMap uses LinkedList to store object, this entry (object of Map.Entry comprise key and value ) will be stored in LinkedList.

How will you retrieve Value object if two Keys will have the same hashcode?
after finding bucket location, we will call keys.equals() method to identify a correct node in LinkedList and return associated value object for that key in Java HashMap.

Read more: http://javarevisited.blogspot...

4.self balancing tree compared with hash table
自平衡的二叉查找树的实现与其竞争对手hash表的实现,各具有优缺点。自平衡二叉查找树在按序遍历所有键值时是量级最优的,hash表不能。自平衡二叉查找树在查找一个键值时,最坏情况下时间复杂度优于hash表, O(log n)对比O(n);但平均时间复杂度逊于hash表,O(log n)对比O(1)。

5. design pattern,最喜欢的design patterns 实现thread-safe 的singleton

  1. 给定词典和一个数据流,搜索单词。 follow up - 构造Trie。

点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
7个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
3年前
Java面试集锦
各位学习Java的同学看过来了,以下是CSDN作者「ThinkWon」总结的面试题。希望对在准备跳槽或者找工作的同学有点帮助。这一波面试题总体来说基本上能够回答面试官的70%问题。虽然有些小节不够完善的,只能靠你自己多面试多总结了。序号内容链接地址1Java基础知识面试题(2020最新版)https://thinkwon.bl
Wesley13 Wesley13
3年前
3 分钟搞瘫阿里内网,拦下13亿次攻击,唯一能让马云睡安稳的男人!
!(https://oscimg.oschina.net/oscnet/ef84ea06b1a64ab383a5e8b963d9261e.jpg)来源:花瓣志(ID:iihuacao)作者:古蒙儿有这样一个人。拿着中学文凭去阿里巴巴面试,结果不但成功了,还成了元老级人物。在阿里待了8年后,又出走自创公司。
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这