Jason Koo

      Stay hungry, Stay foolish!

导航

2013年亚马逊在线笔试题

Posted on 2012-10-25 16:52  Jason Koo  阅读(442)  评论(0编辑  收藏  举报

笔试题有两道,下面分别介绍。实现过程使用了Java语言。

The first one:

We have an array representing customer’s shopping records.
For example, it’s an array like this:
custA, item1,
custB, item1,
custA, item2,
custB, item3,
custC, item1,
custC, item3,
custD, item2,
This array indicates that customer A bought item 1, customer B bought item 1, customer A bought item 2, customer B bought item

3, etc..
For a given item X and shopping records array, write code to find out what else (item Y) was bought mostly by the customers who

bought item X.
For example, in above example, if X is item 1 then Y should be item 3.
Rules:
One customer can only buy one item once.
The mostly brought item should not be item X.
If no customer brought item X, then return “None”
If all the customers who brought item X only brought item X, then return “None”
The first line of input is the item X. The second line of input is the shopping record array, this shopping record array is split by

space.
If there are many other mostly brought items which have equally brought times, then return any one of those items.
Examples:
Input1:
item1
custA item1 custB item1 custA item2 custB item3 custC item1 custC item3 custD item2
Output1:
item3
Input2:
item2
custA item1 custB item1 custC item1 custA item2 custB item3 custA item3
Output2:
item1
(The output2 can be item3 too)

基本思路:顾客和商品之间的关系可以用二分图来表示,考虑数据结构中图的存储有邻接矩阵、邻接表等存储方式。该题需要涉及到每个点的入度和出度计算,应该使用邻接矩阵来存储。可以构建一个二维矩阵,存储顾客和商品之间的关系,顾客作为矩阵的行坐标,商品作为矩阵的纵坐标。顾客购买过商品,则矩阵中相应的元素置为1,其余元素置为0。

实现过程中有个工程性问题,就是输入中的顾客和商品名称都是字符串,如何将其映射为矩阵的行列数值索引,解决了这个问题,基本上就是简单的计数了。名称和索引的对应关系使用了HashTable来存放。具体实现如下:

 1          String line1 = null;
 2         String line2 = null;
 3         //用于存放名称和索引映射关系
 4         Hashtable<String, Integer> custmap = new Hashtable<String, Integer>();
 5         Hashtable<String, Integer> itemmap = new Hashtable<String, Integer>();
 6         
 7         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 8         try {
 9             line1 = br.readLine();
10             line2 = br.readLine();
11         } catch (IOException e) {
12             // TODO Auto-generated catch block
13             e.printStackTrace();
14         }
15         
16         if(!line2.contains(line1)){
17             System.out.println("None");
18             return;
19         }
20         
21         String[] tmp = line2.split(" ");
22         //构建名称和索引的映射关系,并统计顾客和商品数量
23         int m = 0;
24         int n = 0;
25         for(int i = 0; i < tmp.length; i++){
26             if(i % 2 == 0){
27                 if(!custmap.containsKey(tmp[i])){
28                     custmap.put(tmp[i], m++);
29                 }
30             }else {
31                 if(!itemmap.containsKey(tmp[i])){
32                     itemmap.put(tmp[i], n++);
33                 }
34             }
35         }
36         //创建矩阵,用于存放顾客和商品关系
37         int[][] matrix = new int[m][n];
38         //在矩阵中构建顾客和商品关系
39         for(int i = 0; i < tmp.length; i = i+2){
40             int x = custmap.get(tmp[i]);
41             int y = itemmap.get(tmp[i + 1]);
42             matrix[x][y] = 1;
43         }
44         //辅助数组,cust用于标记购买过某一商品的所有顾客,item用于存放cust中标记的所有顾客购买其他商品的数量
45         int cust[] = new int[m];
46         int item[] = new int[n];
47         //获得特定商品在矩阵中的列索引
48         int t = itemmap.get(line1);
49         //标记购买过指定商品的所用顾客
50         for(int i = 0; i < m; i++){
51             if(matrix[i][t] == 1){
52                 cust[i] = 1;
53             }
54         }
55         //统计购买过特定商品的所有顾客购买其他商品的数量,存放在item数组中
56         for(int j = 0; j < n; j++){
57             if(j != t){
58                 for(int i = 0; i < m; i++){
59                     if(cust[i] != 0 && matrix[i][j] == 1){
60                         item[j]++;
61                     }                    
62                 }
63             }
64         }
65         //找到购买数量最多的其他商品,并从映射关系中找到其对应的名称
66         int maxIndex = 0;
67         for(int i = 0; i < n; i++){
68             if(item[i] > item[maxIndex]){
69                 maxIndex = i;
70             }
71         }
72         if(item[maxIndex] == 0){
73             System.out.println("None");
74             return;
75         } else {
76             for(String key : itemmap.keySet()){
77                 if(itemmap.get(key) == maxIndex){
78                     System.out.println(key);
79                     break;
80                 }
81             }
82         }

 

 The second one:

As you know, two operations of Stack are push and pop. Now give you two integer arrays, one is the original array before push and pop operations, the other one is the result array after a series of push and pop operations to the first array. Please give the push and pop operation sequence.

For example:

If the original array is a[] = {1,2,3}, and the result array is b[] = {1,3,2}.

Then, the operation sequence is “push1|pop1|push2|push3|pop3|pop2”(operations are split by ‘|’ and no space).

Rules:

1.The push and pop operations deal with the original int array from left to right..
2.The input is two integer array. They are the original array and the result array. These interger array is split by space..
3.The output is the operation sequence..
4.If the original array cannot make to the result array with stack push and pop, The output should be 'None'..
5.The operation "push1" means push the first element of the original array to the stack..
6.The operation "pop1" means pop the first element of the original array from the stack, and add this element to the tail of the result array..
7.Please don't include any space in the output string..

Sample1:
Input:
1 2 3 4

1 2 3 4
Output:
push1|pop1|push2|pop2|push3|pop3|push4|pop4

Sample2:

Input:

1 2 3 4

4 3 2 1
Output:
push1|push2|push3|push4|pop4|pop3|pop2|pop1

基本思路:程序中使用一个栈,模拟整个操作过程。结果数组下标的移动肯定要慢于初始数组下标,并且当栈顶元素等于结果数组下标所指定的元素时进行出栈操作,并向前移动结果数组下标。具体实现如下所示:

 

 1         public String generateSequence(int[] o, int[] r){//o为初始数组,r为结果数组
 2         String s = "";
 3         int i = 0;
 4         int j = 0;
 5         int len = o.length;
 6         Stack<Integer> stack = new Stack<Integer>();
 7         
 8         while(i < len && j < len){
 9             if(stack.empty()){
10                 stack.push(o[i]);
11                 s += "push" + o[i] + "|";
12                 i++;
13             } else {
14                 if(stack.peek() != r[j]){
15                     stack.push(o[i]);
16                     s += "push" + o[i] + "|";
17                     i++;
18                 } else {
19                     s += "pop" + stack.pop() + "|";
20                     j++;
21                 }
22             }
23         }
24         while( j < len ){
25             if(stack.peek() == r[j]){
26                 s += "pop" + stack.pop() + "|";
27                 j++;
28             } else {
29                 s = "None|";
30                 break;
31             }
32         }
33         return s.substring(0, s.length() - 1);
34     }