amazon 面经1

1st round: 一个白人,先是聊了下project,然后一道coding题,找到距离一个节点k的所有节点。这个直接node bfs就可以解决。这一轮很基础。

node bfs 加 count 计算level就行. 

facebook电面原题

 

2nd round: 一个印度小伙,上来就是题,没有behavior question。


第一题设计手机通讯录用什么数据结构,这题用Trie解决,然后让我实现了Trie的search method。

import java.util.ArrayList;
import java.util.Arrays;
class Node{
    
    String number;
    boolean end;
    Node[] next = new Node[26];
    Node(){
        for(int i=0; i<26;i++)
            next[i] = null;
    }
    public void insert(String s,String number){
        Node cur = this;
        for(int i=0; i<s.length();i++){
            if(cur.next[s.charAt(i)-'a'] == null)
            {
                cur.next[s.charAt(i)-'a'] = new Node();
            }
            cur = cur.next[s.charAt(i)-'a'];
        }
        cur.end = true;
        cur.number = number;            
    }
    
    public String search(String s){
        Node cur = this;
        for(int i=0; i<s.length();i++){
            if(cur.next[s.charAt(i)-'a'] == null)
            {
                return null;
            }
            cur = cur.next[s.charAt(i)-'a'];
        }
        if(cur.end != true)
            return null;
        else
            return cur.number;
    }
}


public class Solution {


        public static void main(String[] args) {
            Node head = new Node();
            head.insert("abc", "323-1");
            head.insert("bcde", "323-2");
            head.insert("abcfs", "323-3");
            head.insert("z", "323-4");
            head.insert("abcfggg", "323-5");
            
            System.out.print(head.search("bcde"));
        }
    

}

 


第二题是关于集合intersection的,有两个集合,要求找出在第一个集合出现p次,在第二个集合出现q次的所有元素。这个用hashtable就能解决。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.swing.text.html.HTMLDocument.Iterator;
class Intersection{
    public ArrayList<Integer> findIntersection(int[] ary1, int[] ary2, int p1, int p2){
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        ArrayList<Integer> ret = new ArrayList<Integer>();
        for(int i=0; i<ary1.length;i++){
            if(!map.containsKey(ary1[i])){
                map.put(ary1[i], 1);
            }else{
                int count = map.get(ary1[i]);
                count++;
                map.put(ary1[i], count);
            }
        }
        //delete all from array1 where values not equal p1
        //java.util.ConcurrentModificationException if direct delete from map.
        HashMap<Integer,Integer> map2 = new HashMap<Integer,Integer>();
        for(Entry<Integer,Integer> entry : map.entrySet()){
            if(entry.getValue()==p1)
                map2.put(entry.getKey(), 0);
        }
        for(int i=0; i<ary2.length;i++){
            if(!map2.containsKey(ary2[i])){
                continue;
            }else{
                int count = map2.get(ary2[i]);
                count++;
                map2.put(ary2[i], count);
            }
        }
        for(Entry<Integer,Integer> entry : map2.entrySet()){
            if(entry.getValue()==p2)
                ret.add(entry.getKey());
        }
        return ret;
    }
    
}


public class Solution {


        public static void main(String[] args) {
            Intersection inter = new Intersection();
            int[] ary1 = {1,2,2,2,3,3,3,4,5,6};
            int[] ary2 = {111,1,1,2,2,3,3,44,4,4,5,5,6};
            ArrayList<Integer> r = inter.findIntersection(ary1, ary2, 3, 2);
            for(int i=0;i<r.size();i++){
                System.out.print(" " + r.get(i));
            }
        }
    

}

 

第三题  the occurences of a substring in a string

kmp 待续

 

第三轮   这一轮面的是设计题。首先聊了下简历,让我介绍了一下我简历上一个关于设计的project。
然后就是让我design一个elevator。设计题总感觉只要说得有道理就行,我就不说我的设计了

 

elevator System:

http://stackoverflow.com/questions/493276/modelling-an-elevator-using-object-oriented-analysis-and-design

Solution
Lets break it into objects.

Elevator 
First there is an elevator class.
It has a direction (up, down, stand, maintenance), 
a current floor and 
a list of floor requests sorted in the direction. 
It receives request from this elevator.
ElevatorState
doors
Passenger it has boarded, also we need to have addPassenger function, and load it can take

Bank or ElevatorController
Then there is a bank. It contains the elevators and receives the requests from the floors. These are scheduled to all active elevators (not in maintenance).
The scheduling will be like:
if available pick a standing elevator for this floor.
else pick an elevator moving to this floor.
else pick a standing elevator on an other floor.
else pick the elevator with the lowest load.
ElevatorState enum 
Each elevator has a set of states. 
Maintenance: the elevator does not react to external signals (only to its own signals).
Stand: the elevator is fixed on a floor. If it receives a call. And the elevator is on that floor, the doors open. If it is on another floor, it moves in that direction.
Up: the elevator moves up. Each time it reaches a floor, it checks if it needs to stop. If so it stops and opens the doors. It waits for a certain amount of time and closes the door (unless someting is moving through them. Then it removes the floor from the request list and checks if there is another request. If so the elevator starts moving again. If not it enters the state stand.
Down: like up but in reverse direction.
There are additional signals:
alarm. The elevator stops. And if it is on a floor, the doors open, the request list is cleared, the requests moved back to the bank.
door open. Opens the doors if an elevator is on a floor and not moving.
door closes. Closed the door if they are open.
Some elevators don't start at bottom/first_floor esp. in case of sky scrappers. min_floor & max_floor are two additional attributes for Elevator.

Elevator 

https://github.com/shabbirv/svhm-elevatorsimulation/blob/master/ElevatorSVHM/src/Elevator/ElevatorImpl.java

 

第四轮 一个白人manager,应该是bar raiser。开始问了很多behavior和简历的问题。然后是一道coding一道设计题。.
coding题是valid words of letter combinations,就是leetcode上的题稍微改了一下。写完之后问了如何改进,我想了想说因为用的是recursion,If there 

Then you can add additional conditions for pruning。还可以通过cashing the result来improve。

 

public class Solution {
    static Map<Character, String> letterMap = new HashMap<Character, String>();
    static{
        letterMap.put('1', "");
        letterMap.put('2', "abc");
        letterMap.put('3', "def");
        letterMap.put('4', "ghi");
        letterMap.put('5', "jkl");
        letterMap.put('6', "mno");
        letterMap.put('7', "pqrs");
        letterMap.put('8', "tuv");
        letterMap.put('9', "wxyz");
    }
    
    public ArrayList<String> letterCombinations(String digits) {
        char[] cs = new char[digits.length()];
        ArrayList<String> res = new ArrayList<String>();
        appendDigits(digits, 0, cs, res);
        return res;
    }
    
    private void appendDigits(String digits, int i, char[] cs, ArrayList<String> res){
        if(i == digits.length()){
            res.add(new String(cs));
            return;
        }
        String letters = letterMap.get(digits.charAt(i));
        for(int j = 0; j < letters.length(); j++){
            cs[i] = letters.charAt(j);
            appendDigits(digits, i + 1, cs, res);
        }
    }
}

 

第二题 设计题是关于amazon recommendation system的,如何通过history data来进行recommendation。我提到了可以用类似于LRUCache的结构获得最近的. from: 1point3acres.com/bbs 
历史数据,然后根据各个产品的销量来进行推荐。还有如果用户可以提供feedback的话,也可以利用feedback来推荐。然后问我如何获取一个recommendation
rank list。我想了想说可以对不同影响因素设定权值,然后计算进行排序就可获得。
这一轮感觉不是很好,第二个设计题感觉答得很烂,基本就是自己想到哪说到哪。所以面完之后感觉要跪。

 

use the items that customer purchase rate represent their interests but other attributes including item viewed, subject interests, demographic data and favorite artists.


most recommendation algorithms start by finding a set of customers who purchased and rated items overlap the rated purchased and rated items.algorithm get items from there similar customers, eliminate items the user has already purchased or rated. and recommends the remaining items.

collaborative filtering
represent a customer as an N-dimensional vector of items, where N is the number of distinct catalog items. the components of the vector are positive for purchased or positively related items and negative for negatively related item. to compensate for best-selling items, the algorithm typically multiplies the vector components by the inverse frequency(the inverse of the number of customers who have purchased or rated the item, making less well-known items much more relevant).
generate recommendations based on a few customers who are most similar to the users. a common method is to measure the cosine of the angle between the two vectors.

A-> * b->
-------------
|A|*|B|

runtime O(MN)  M  is number of customs, and N is the number of the product catalog items. 
unfortunately, method reduce recommendation quality in several ways.
First. if only a small simple, the select customers will be less similar to the user.
Second, restrict recommendation to a specific product or subject area
third, of the algorithm discard the most popular or unpopular items, they will never appear as recommendations.




===========================================
item to item collaborative filtering, scales to massive data sets and produces high-quality recommendations in real time


rather than matching the user to similar customers, item-to-item collaborative filtering match each the user's purchased and related items to similar items. then combines those similar items into a recommendation list.

build a similar-items table by finding items that customers tend to purchase together . calculate similarity between a single product and all related product

For each item in product catalog, I1 
  For each customer C who purchased I1
     For each item I2 purchased by customer C 
       Record that a customer purchased I1 and I2 
     For each item I2 Compute the similarity between I1 and I2


its possible to compute the similarity between two items.
use the cosine measure compute the similarity.
each vector corresponds to an item rahter than a customer, and vector's M dimensions
conrrespond to customers who have purchased that item.

this offline computation is worst tiem O(N^2M) but person buy little stuff. so O(NM). give a similar-items table , finds items similar to each user's purchases and rating. recommends the most popular or correlated items. Scalability: create the expensive similar-item table offline. scales independently of the catalog size or the total number of customers. it is dependent only on how many titles the user has purchased or rated..

 

 

 

 

 

posted on 2014-08-07 04:37  brave_bo  阅读(688)  评论(0编辑  收藏  举报

导航