亚马逊在线技术笔试(Amazon Hiring Campus 2013 - Final 6)

刚参加了亚马逊的在线笔试,果断酱油党啊。。。

两个小时两道题目。可选语言有C、C++、Java、PHP、Python、C#、Ruby、Perl、Haskell、Scala,要求全部使用标准输入输出,已给出部分代码,只要填上关键代码即可,而且不能再自己增加函数。

完了他那个页面上有个compile&test按钮,test一下有几个testcase,然后只有第一个testcase的expected output是可见的,其他全部是hiding的。第一题有9个testcase,可怜我第一次提交只有3个通过,修改完也只有4个通过。。囧。。第二题有7个testcase,第二题果然时间不够了没写完。。。。

 

下面看看题目:

Question 1 / 2

Let's assume that there is a simple market for beans. Every day there is a published bean price in the market. Traders can buy or sell at the published price. There is a trader who time travelled to future and brought back the price information for a number of days in the future. If you have this information and you are allowed to buy and sell many times. How do you make the maximum profit? The price information will be given as an array of numbers. Each number is for a day’s trading price. The numbers are all integers to simplify the problem. You will need to return the index of the buy-in point and sell-out point for maximum profit.

Rules:

1) The input line length less than 1000, and the trading price length less than 100;

2) The trading price is positive integer;

3) The trading prices are delimited  by ' '(single space);

4) Please make sure every buying and selling period shortest. especially, please ouput '-' if all the trading prices are the same or no trading point;

Sample Input and Output:

Input 1

1 3 5 4 2 8 10

Output 1

1 3 5 7

To make the maximum profit, you should buy at $1 and sell at $5, and then buy at $5 and sell it at $10. so the output is "1 3 5 7".

Input 2 

1 1 1 3 5 4 2 2 2 8 10

Ouput 2

3 5 9 11 

 

已给的C代码为:

/ 
//C Sample 
 
#include <stdio.h> 
#include <string.h> 

void calculateAndPrint(int array[], int length){ 
//Your Code is here 
printf("-"); 
} 

int splitAndConvert(char* strings,int array[]){ 
char*tokenPtr = strtok(strings," "); 
int i=0; 
while(tokenPtr!=NULL){ 
array[i] = atoi(tokenPtr); 
i++; 
tokenPtr=strtok(NULL," "); 
} 
return i; 
} 

int main(){ 
char line[1000] = {0} ; 
while(gets(line)){ 
int array[100] = {0}; 
int length = splitAndConvert(line,array); 
if(length==0){ 
break; 
} 
calculateAndPrint(array, length); 
printf("\n"); 
} 
return 0; 
}   

 

 

 

Question 2 / 2

consider a kind of configuration file in amazon sofeware system. this kind of configuration file's format looks like this:

B=10;

A={

  A=100;

  B=BDE;

  C=C;

  D={

     A=windows;

     B=mac;

     C={

       A=redhat;

       B=ubuntu;

      };

 };

A+={

 A=200;

 E=1000;

};

to repsent the key of the configuration, we use period(.) delimated method. for example,A.B represents the element B in Map A, and  the value of A.B is BDE; similarly,  the value of A.D.C.A is redhat. the the represent strin

...



2小时,两个题目。在线编程,英文题目。当时没做好,完了自己把他们完成了。答案是我自己写的,自己测试没问题,若有错误请指正。
                                                         Question 1 / 2
Question:
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:
1.  One customer can only buy one item once.
2.  The mostly brought item should not be item X.
3.  If no customer brought item X, then return “None”
4.  If all the customers who brought item X only brought item X, then return “None”
5.  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.
6.  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. /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <set>
  6. #include <algorithm>

  7. #include <cstring>
  8. #include <cstdio>

  9. using namespace std;

  10. char* findMostlyBroughtItem(char* shippingRecordArray[], int length, char* givenItem);

  11. inline bool isSpace(char x){
  12.     return x == ' ' || x == '\r' || x == '\n' || x == '\f' || x == '\b' || x == '\t';
  13. }

  14. char * rightTrim(char *str){
  15.     int len = strlen(str);
  16.     while(--len>=0){
  17.         if(isSpace(str[len])){
  18.             str[len] = '\0';
  19.         }else{
  20.             break;
  21.         }
  22.     }
  23.     return str;
  24. }

  25. char * getInputLine(char *buffer, int length){
  26.     if(fgets(buffer,length, stdin)==NULL){
  27.         return NULL;
  28.     }
  29.     rightTrim(buffer);
  30.     if(strlen(buffer)<=0){
  31.         return NULL;
  32.     }
  33.     return buffer;
  34. }

  35. int splitAndConvert(char* strings,char* array[]){
  36.     char*tokenPtr = strtok(strings," ");
  37.     int i=0;
  38.     while(tokenPtr!=NULL){
  39.         array[i] = tokenPtr;
  40.         i++;
  41.         tokenPtr=strtok(NULL," ");
  42.     }
  43.     return i;
  44. }

  45. int main()
  46. {
  47.     char givenItem[1000] = {0} ;
  48.     while(getInputLine(givenItem, 1000)){
  49.     char line[1000];
  50.     getInputLine(line, 1000);
  51.         
  52.     char* shoppingRecordArray[1000] = {0};
  53.         
  54.         int length = splitAndConvert(line,shoppingRecordArray);
  55.         if(length==0){
  56.             break;
  57.         }
  58.         

  59.         char * item = findMostlyBroughtItem(shoppingRecordArray, length, givenItem);
  60.         if (NULL != item)
  61.         {   // 原来系统提供的代码。这里没有NULL判断
  62.             cout<<item<<endl;
  63.             free(item) // 自己加的
  64.         }
  65.     }
  66.     return 0; 
  67. }

  68. void
  69. print(pair<string, int> p) {
  70.     cout << p.first << p.second << endl;
  71. }

  72. //your code is here 
  73. //下面才是让写代码的地方,其他的系统已经自动给出。主函数,只有一点点修改。

  74. char* findMostlyBroughtItem(char* shoppingRecordArray[], int length, char* givenItem)
  75. {
  76.     if (NULL == shoppingRecordArray || NULL == givenItem)
  77.         return NULL;
  78.     
  79.     string obj_item(givenItem);
  80.     // 将用户信息 与 购买商品信息 存入multimap record
  81.     multimap<string, string> record;
  82.     for (int i = 0; i < length; i += 2)
  83.     {
  84.         string customer(shoppingRecordArray[i]);
  85.         string item(shoppingRecordArray[i+1]);

  86.         record.insert(pair<string, string>(customer, item));
  87.     }

  88.     // 提取出购买了obj_item商品的客户名称集合 customers
  89.     set<string> customers;
  90.     for (map<string, string>::iterator it = record.begin(); it != record.end(); it++)
  91.     {
  92.         if (== (*it).second.compare(obj_item))
  93.         {
  94.             customers.insert((*it).first);
  95.         }
  96.     }
  97.     // 遍历购买记录 multimap record
  98.     // 若客户名称 在 集合set customers 存在,则将商品插入map result
  99.     map<string, int> result;
  100.     for (map<string, string>::iterator it = record.begin(); it != record.end(); it++)
  101.     {
  102.         for (set<string>::iterator ic = customers.begin(); ic != customers.end(); ic++)
  103.         {
  104.             if (== (*it).first.compare(*ic))
  105.             {
  106.                 /*
  107.                 if (result.end() != result.find((*it).second))
  108.                 {
  109.                     result[(*it).second] += 1;
  110.                 }
  111.                 else
  112.                     result.insert(pair<string, int>((*it).second, 1));
  113.                     */
  114.                 result[(*it).second] += 1;
  115.                 break;
  116.             }
  117.         }

  118.     }


  119.     pair<string, int> top("None", 0);
  120.     // 遍历map result, 寻找最大,而非obj_item的商品名称
  121.     for (map<string, int>::iterator it = result.begin(); it != result.end(); it++)
  122.     {
  123.         if (== (*it).first.compare(obj_item))
  124.             continue;
  125.         if ((*it).second > top.second)
  126.             top = make_pair((*it).first, (*it).second);
  127.     }

  128.     //cout << "Top: " << top.first << "\t" << top.second << endl;
  129.     
  130.     char *= (char *)malloc(top.first.length() + 1);
  131.     if (NULL != p)
  132.     {
  133.         strcpy(p, top.first.c_str());
  134.         return p;
  135.     }

  136.     return NULL;
  137. }

                                                       Question 2 / 2
Question:
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:
Time Remaining: 00:25:17
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. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <stack>

  6. using namespace std;

  7. char* calculateOperationSequence(int *originalArray, int *resultArray, int length);

  8. inline bool isSpace(char x){
  9.     return x == ' ' || x == '\r' || x == '\n' || x == '\r' || x == '\b' || x == '\t';
  10. }

  11. char * rightTrim(char *str){
  12.     int len = strlen(str);
  13.     while(--len>=0){
  14.         if(isSpace(str[len])){
  15.             str[len] = '\0';
  16.         }else{
  17.             break;
  18.         }
  19.     }
  20.     return str;
  21. }

  22. char * getInputLine(char *buffer, int length){
  23.     if(fgets(buffer,length, stdin)==NULL){
  24.         return NULL;
  25.     }
  26.     rightTrim(buffer);
  27.     if(strlen(buffer)<=0){
  28.         return NULL;
  29.     }
  30.     return buffer;
  31. }

  32. int splitAndConvert(char* strings,int *array){
  33.     char*tokenPtr = strtok(strings,",");
  34.     int i=0;
  35.     while(tokenPtr!=NULL){
  36.         array[i] = atoi(tokenPtr);
  37.         i++;
  38.         tokenPtr=strtok(NULL,",");
  39.     }
  40.     return i;
  41. }

  42. int main(){
  43.     char line[1000] = {0} ;
  44.     while(getInputLine(line,1000)){
  45.         int originalArray[30] = {0};
  46.         int originalArrayLength = splitAndConvert(line,originalArray);
  47.         if(originalArrayLength==0){
  48.             break;
  49.         }
  50.         
  51.     getInputLine(line, 1000);
  52.     int resultArray[30] = {0};
  53.         int resultArrayLength = splitAndConvert(line,resultArray);
  54.         if(resultArrayLength==0){
  55.             break;
  56.         }
  57.         char *operationSequence = calculateOperationSequence(originalArray, resultArray, resultArrayLength);

  58.     if (NULL != operationSequence)
  59.     {   // 原来系统提供的代码。这里没有NULL判断
  60.         cout<< operationSequence <<endl;
  61.         free(operationSequence); // 自己加的
  62.     }
  63.     else
  64.      cout<< "None" <<endl; // 自己加的
  65.     }
  66.     return 0; 
  67. } 

  68. //your code is here  
  69. //下面才是让写代码的地方,其他的系统已经自动给出。主函数,只有一点点修改。
  70. char* calculateOperationSequence(int * originalArray, int * resultArray, int length)
  71. {
  72.     if (NULL == originalArray || NULL == resultArray || length <= 0)
  73.         return NULL;
  74.     //使用一个栈模拟入栈和出栈操作就ok了。
  75.     string str;
  76.     stack<int> st;
  77.     int i = 0;
  78.     int j = 0;
  79.     st.push(originalArray[i]);


  80.     char tmp[5] = "\0";
  81.     str.append("push");
  82.     sprintf(tmp, "%d", originalArray[i]);
  83.     str.append(tmp);
  84.     str.append("|");

  85.     i++;

  86.     while (!st.empty())
  87.     {
  88.         if (< length && st.top() == resultArray[j])
  89.         {
  90.             str.append("pop");
  91.             sprintf(tmp, "%d", resultArray[j]);
  92.             str.append(tmp);
  93.             str.append("|");
  94.             st.pop();
  95.             j++;
  96.             
  97.             if (< length)
  98.             {
  99.                 st.push(originalArray[i]);
  100.                 str.append("push");
  101.                 sprintf(tmp, "%d", originalArray[i]);
  102.                 str.append(tmp);
  103.                 str.append("|");
  104.                 i++;
  105.             }
  106.         }
  107.         else
  108.         {
  109.             if (< length)
  110.             {
  111.                 st.push(originalArray[i]);
  112.                 str.append("push");
  113.                 sprintf(tmp, "%d", originalArray[i]);
  114.                 str.append(tmp);
  115.                 str.append("|");
  116.                 i++;
  117.             }
  118.             else
  119.                 break;
  120.         }
  121.     }


  122.     if (!st.empty())
  123.         return NULL;

  124.     char *= (char *)malloc(+ str.length());
  125.     if (NULL != p)
  126.     {
  127.         strcpy(p, str.c_str());
  128.         p[str.length() - 1] = '\0';
  129.         return p;
  130.     }

  131.     return NULL;
  132. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值