https://instant.1point3acres....
1.Product of Array Except Self
2.給兩天的用戶log數據,求兩天均登錄Amazon的用戶,follow up - log很大不能夠放入內存
相關問題,統計訪問量最高的前10個ip,內存不夠怎麼辦。統計最高的單詞,內存不夠。html
(大數據處理,當內存不夠的時候,考慮取模後映射成小文件,而後再用hash分別統計,而後再堆排序)java
Time is O(n*log(k)).node
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.app
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.ide
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.this
Read more: http://javarevisited.blogspot...code
4.self balancing tree compared with hash table
自平衡的二叉查找樹的實現與其競爭對手hash表的實現,各具備優缺點。自平衡二叉查找樹在按序遍歷全部鍵值時是量級最優的,hash表不能。自平衡二叉查找樹在查找一個鍵值時,最壞狀況下時間複雜度優於hash表, O(log n)對比O(n);但平均時間複雜度遜於hash表,O(log n)對比O(1)。orm
5. design pattern,最喜歡的design patterns 實現thread-safe 的singletonhtm
給定詞典和一個數據流,搜索單詞。 follow up - 構造Trie。