---
title: "amazon题代码"
author: "vergilwang"
date: "2013-04-25T16:01:00Z"
tags: []
categories: ["互联网"]
slug: "amazon"
draft: false
lastmod: "2014-02-15T14:58:01Z"
id: 2011226
url: "https://vergilwang.iteye.com/blog/2011226"
---

First Phone Interview:  
1.提出尽可能多的方法使一个method可以返回多个不同type的值  
2.reverse string  
比如 "I have a dream" -&gt; "dream a have I"  
3.判断一个binary tree是不是对称的

Second Phone Interview:  
1.给a list of number，返回前top K个（内存足够怎么做，内存不够怎么做）  
2.用OOD来电梯  
3.找两个链表的交集

Onsite 6轮 1轮HR 1轮午餐 4轮技术 （亚马逊web services team)  
1.设计个电话本, 可以用那些数据结构  
答案貌似可以用suffix tree或者hashtable  
2.问research, OOD 交通灯系统  
3.写函数算一个整数的阶层 n!, 又问了n很大，怎么办？  
比如99%的n都在400000-900000之间，怎么提高函数的执行速度  
4.给一个数组和一个数n，找出数组中的所有的对和等于n的  
5.给手机键盘，给定某个按键序列比如'1489'，返回这个按键序列生成的所有的正确单词

1.判断一年是否为闰年的方法

 

**\[cpp]**[view plain](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "view plain")[copy](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "copy")

 

01. #include"stdio.h"
02. intisRun(intnum)
03. {
04. intret=0;
05. if(num%4==0)
06. {
07. ret=1;
08. if(num%100==0)
09. {
10. ret=0;
11. }
12. if(num%400==0)
13. {
14. ret=1;
15. }
16. }
17. returnret;
18. }
19. intmain()
20. {
21. intyear;
22. scanf("%d",&year);
23. if(isRun(year))
24. {
25. printf("run\\n");
26. }
27. else
28. {
29. printf("no\\n");
30. }
31. return0;
32. }

2 Please write a program which can print all 6 digitsnumbers composed of 1, 2, 2,3,4,5.

 

 

**\[cpp]**[view plain](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "view plain")[copy](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "copy")

 

01. #include"stdio.h"
02. voidshowAnother(char\*str,intstart,intnum)
03. {
04. inti=0;
05. charc;
06. if(str\[start]=='\\0')
07. {
08. printf("%s\\n",str);
09. }
10. if(str==NULL)
11. return;
12. for(i=start;i&lt;num;i++)
13. {
14. c=str\[start];
15. str\[start]=str\[i];
16. str\[i]=c;
17. showAnother(str,start+1,num);
18. c=str\[start];
19. str\[start]=str\[i];
20. str\[i]=c;
21. }
22. }
23. voidshowDigit(char\*str,intstart,intnum)
24. {
25. inti=0;
26. charc;
27. if(str==NULL)
28. return;
29. for(i=start;i&lt;num;i++)
30. {
31. c=str\[start];
32. str\[start]=str\[i];
33. str\[i]=c;
34. showAnother(str,start+1,num);
35. c=str\[start];
36. str\[start]=str\[i];
37. str\[i]=c;
38. }
39. }
40. intmain()
41. {
42. charstr\[]="122345";
43. showDigit(str,0,sizeof(str)/sizeof(str\[0]));
44. }

3题目：输入一个英文句子，翻转句子中单词的顺序，但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见，标点符号和普通字母一样处理。

 

例如输入“I am astudent.”，则输出“student. a am I”。

 

**\[cpp]**[view plain](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "view plain")[copy](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "copy")

 

01. #include"stdio.h"
02. #include"string.h"
03. #defineMAX1024
04. #defineCHARLEN256
05. voidstrReverse(char\*str,intstart,intlen)
06. {
07. intbegin;
08. intend;
09. inttemp,i;
10. charc;
11. if(str==NULL)
12. return;
13. begin=start;
14. for(end=start;end&lt;=len;end++)
15. {
16. if(str\[end]==''||str\[end]=='\\0')
17. {
18. temp=end;
19. temp-=1;
20. for(;begin&lt;temp;begin++,temp--)
21. {
22. c=str\[temp];
23. str\[temp]=str\[begin];
24. str\[begin]=c;
25. }
26. begin=end+1;
27. }
28. }
29. end=len-1;
30. for(i=start;i&lt;end;i++,end--)
31. {
32. c=str\[i];
33. str\[i]=str\[end];
34. str\[end]=c;
35. }
36. }
37. intmain()
38. {
39. charstr\[MAX];
40. inti;
41. intlen;
42. gets(str);
43. len=strlen(str);
44. strReverse(str,0,len);
45. printf("%s\\n",str);
46. return0;
47. }

4题目：输入两个字符串，从第一字符串中删除第二个字符串中所有的字符。例如，输入”Theyare students.”和”aeiou”，则删除之后的第一个字符串变成”Thy r stdnts.”。

 

 

**\[cpp]**[view plain](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "view plain")[copy](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "copy")

 

01. #include"stdio.h"
02. #include"string.h"
03. #defineMAX1024
04. #defineCHARLEN256
05. intmain()
06. {
07. charstr\[MAX];
08. charstrToMove\[MAX];
09. intmask\[CHARLEN];
10. inti;
11. intlen;
12. intlenToMove;
13. memset(mask,0,sizeof(int)\*CHARLEN);
14. gets(str);
15. getchar();
16. scanf("%s",strToMove);
17. len=strlen(str);
18. lenToMove=strlen(strToMove);
19. for(i=0;i&lt;lenToMove;i++)
20. {
21. mask\[strToMove\[i]]=1;
22. }
23. for(i=0;i&lt;len;i++)
24. {
25. if(!mask\[str\[i]])
26. printf("%c",str\[i]);
27. }
28. putchar('\\n');
29. }

5 求字符串所有子字符串（顺序不同算一个）

题目大致意思要求一个字符串所有子字符串（长度从1到总长，但顺序不同，如ab和ba算一个）

此题有个解法可以利用二进制处理【只能是当字符串中不出现重复字符的时候】

 

 

**\[cpp]**[view plain](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "view plain")[copy](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "copy")

 

01. #include"stdio.h"
02. #include"string.h"
03. #include"math.h"
04. #defineMAX1024
05. #defineCHARLEN256
06. intmain()
07. {
08. charstr\[MAX];
09. intlen;
10. intnumber;
11. inti,j;
12. scanf("%s",str);
13. len=strlen(str);
14. number=pow(2,len);
15. printf("Allstrlist\\n");
16. for(i=1;i&lt;number;i++)
17. {
18. for(j=0;j&lt;len;j++)
19. {
20. if((i&gt;&gt;j)&1)
21. {
22. putchar(str\[j]);
23. }
24. }
25. putchar('\\n');
26. }
27. }

![](http://img.my.csdn.net/uploads/201209/19/1348068730_6368.jpg)

 

当字符串重复的时候, 先排序nlogn，此时重复的字符会变成连续的，bcaeb 排序以后,abbce, 在用O(n)把排序数组去重，再用上面方法。

 

**6. Swap two nodes in list**

SWAP(node \*head, node \*first, node \*second)

这道题目 还要构建节点比较复杂，题目的意思 就是交换两个节点。

当然这道题目 需要指针操作。

不能进行值的交换，因为类型不明哦。

 

7**Find Longest Repeat SubString ,可以overlap，区分大小写**

这道题一看就是典型的后缀数组的题目

应该比较简单

我跑一遍试试。

 

**\[cpp]**[view plain](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "view plain")[copy](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "copy")

 

01. #include"stdio.h"
02. #include"string.h"
03. #include"math.h"
04. #include"assert.h"
05. #defineMAX1024
06. #defineCHARLEN256
07. intPartion(char\*str\[MAX],ints,inte)
08. {
09. inti=s-1;
10. intj;
11. char\*p=str\[e];
12. char\*temp;
13. for(j=s;j&lt;e;j++)
14. {
15. if(strcmp(str\[j],p)&lt;0)
16. {
17. temp=str\[i+1];
18. str\[i+1]=str\[j];
19. str\[j]=temp;
20. i+=1;
21. }
22. }
23. temp=str\[i+1];
24. str\[i+1]=p;
25. str\[e]=temp;
26. returni+1;
27. }
28. voidquickSort(char\*str\[MAX],intstart,intend)
29. {
30. intq;
31. if(start&lt;end)
32. {
33. q=Partion(str,start,end);
34. quickSort(str,start,q-1);
35. quickSort(str,q+1,end);
36. }
37. }
38. intcomLen(char\*str1,char\*str2)
39. {
40. intcount=0;
41. assert(str1&&str2);
42. while(\*str1==\*str2)
43. {
44. if(\*str1=='\\0'||\*str2=='\\0')
45. break;
46. count++;
47. str1++;
48. str2++;
49. }
50. returncount;
51. }
52. intmain()
53. {
54. charstr\[MAX];
55. charanswer\[MAX];
56. char\*strPoint\[MAX];
57. inti,len;
58. intmaxLen,clen;
59. scanf("%s",str);
60. len=strlen(str);
61. for(i=0;i&lt;len;i++)
62. {
63. strPoint\[i]=str+i;
64. }
65. quickSort(strPoint,0,len-1);
66. maxLen=0;
67. for(i=0;i&lt;len-1;i++)
68. {
69. clen=comLen(strPoint\[i],strPoint\[i+1]);
70. if(clen&gt;maxLen)
71. {
72. strncpy(answer,strPoint\[i],clen);
73. answer\[clen]='\\0';
74. maxLen=clen;
75. }
76. }
77. printf("%s\\n",answer);
78. }

用快排+后缀数组来解决的，还好测试的比较顺利

 

![](http://img.my.csdn.net/uploads/201209/20/1348070778_7261.jpg)

**分享到：**[]()[]()

 

 

First Phone Interview:  
1.提出尽可能多的方法使一个method可以返回多个不同type的值  
2.reverse string  
比如 "I have a dream" -&gt; "dream a have I"  
3.判断一个binary tree是不是对称的

Second Phone Interview:  
1.给a list of number，返回前top K个（内存足够怎么做，内存不够怎么做）  
2.用OOD来电梯  
3.找两个链表的交集

Onsite 6轮 1轮HR 1轮午餐 4轮技术 （亚马逊web services team)  
1.设计个电话本, 可以用那些数据结构  
答案貌似可以用suffix tree或者hashtable  
2.问research, OOD 交通灯系统  
3.写函数算一个整数的阶层 n!, 又问了n很大，怎么办？  
比如99%的n都在400000-900000之间，怎么提高函数的执行速度  
4.给一个数组和一个数n，找出数组中的所有的对和等于n的  
5.给手机键盘，给定某个按键序列比如'1489'，返回这个按键序列生成的所有的正确单词

1.判断一年是否为闰年的方法

 

**\[cpp]**[view plain](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "view plain")[copy](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "copy")

 

01. #include"stdio.h"
02. intisRun(intnum)
03. {
04. intret=0;
05. if(num%4==0)
06. {
07. ret=1;
08. if(num%100==0)
09. {
10. ret=0;
11. }
12. if(num%400==0)
13. {
14. ret=1;
15. }
16. }
17. returnret;
18. }
19. intmain()
20. {
21. intyear;
22. scanf("%d",&year);
23. if(isRun(year))
24. {
25. printf("run\\n");
26. }
27. else
28. {
29. printf("no\\n");
30. }
31. return0;
32. }

2 Please write a program which can print all 6 digitsnumbers composed of 1, 2, 2,3,4,5.

 

 

**\[cpp]**[view plain](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "view plain")[copy](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "copy")

 

01. #include"stdio.h"
02. voidshowAnother(char\*str,intstart,intnum)
03. {
04. inti=0;
05. charc;
06. if(str\[start]=='\\0')
07. {
08. printf("%s\\n",str);
09. }
10. if(str==NULL)
11. return;
12. for(i=start;i&lt;num;i++)
13. {
14. c=str\[start];
15. str\[start]=str\[i];
16. str\[i]=c;
17. showAnother(str,start+1,num);
18. c=str\[start];
19. str\[start]=str\[i];
20. str\[i]=c;
21. }
22. }
23. voidshowDigit(char\*str,intstart,intnum)
24. {
25. inti=0;
26. charc;
27. if(str==NULL)
28. return;
29. for(i=start;i&lt;num;i++)
30. {
31. c=str\[start];
32. str\[start]=str\[i];
33. str\[i]=c;
34. showAnother(str,start+1,num);
35. c=str\[start];
36. str\[start]=str\[i];
37. str\[i]=c;
38. }
39. }
40. intmain()
41. {
42. charstr\[]="122345";
43. showDigit(str,0,sizeof(str)/sizeof(str\[0]));
44. }

3题目：输入一个英文句子，翻转句子中单词的顺序，但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见，标点符号和普通字母一样处理。

 

例如输入“I am astudent.”，则输出“student. a am I”。

 

**\[cpp]**[view plain](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "view plain")[copy](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "copy")

 

01. #include"stdio.h"
02. #include"string.h"
03. #defineMAX1024
04. #defineCHARLEN256
05. voidstrReverse(char\*str,intstart,intlen)
06. {
07. intbegin;
08. intend;
09. inttemp,i;
10. charc;
11. if(str==NULL)
12. return;
13. begin=start;
14. for(end=start;end&lt;=len;end++)
15. {
16. if(str\[end]==''||str\[end]=='\\0')
17. {
18. temp=end;
19. temp-=1;
20. for(;begin&lt;temp;begin++,temp--)
21. {
22. c=str\[temp];
23. str\[temp]=str\[begin];
24. str\[begin]=c;
25. }
26. begin=end+1;
27. }
28. }
29. end=len-1;
30. for(i=start;i&lt;end;i++,end--)
31. {
32. c=str\[i];
33. str\[i]=str\[end];
34. str\[end]=c;
35. }
36. }
37. intmain()
38. {
39. charstr\[MAX];
40. inti;
41. intlen;
42. gets(str);
43. len=strlen(str);
44. strReverse(str,0,len);
45. printf("%s\\n",str);
46. return0;
47. }

4题目：输入两个字符串，从第一字符串中删除第二个字符串中所有的字符。例如，输入”Theyare students.”和”aeiou”，则删除之后的第一个字符串变成”Thy r stdnts.”。

 

 

**\[cpp]**[view plain](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "view plain")[copy](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "copy")

 

01. #include"stdio.h"
02. #include"string.h"
03. #defineMAX1024
04. #defineCHARLEN256
05. intmain()
06. {
07. charstr\[MAX];
08. charstrToMove\[MAX];
09. intmask\[CHARLEN];
10. inti;
11. intlen;
12. intlenToMove;
13. memset(mask,0,sizeof(int)\*CHARLEN);
14. gets(str);
15. getchar();
16. scanf("%s",strToMove);
17. len=strlen(str);
18. lenToMove=strlen(strToMove);
19. for(i=0;i&lt;lenToMove;i++)
20. {
21. mask\[strToMove\[i]]=1;
22. }
23. for(i=0;i&lt;len;i++)
24. {
25. if(!mask\[str\[i]])
26. printf("%c",str\[i]);
27. }
28. putchar('\\n');
29. }

 

5 求字符串所有子字符串（顺序不同算一个）

题目大致意思要求一个字符串所有子字符串（长度从1到总长，但顺序不同，如ab和ba算一个）

此题有个解法可以利用二进制处理【只能是当字符串中不出现重复字符的时候】

 

 

**\[cpp]**[view plain](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "view plain")[copy](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "copy")

 

01. #include"stdio.h"
02. #include"string.h"
03. #include"math.h"
04. #defineMAX1024
05. #defineCHARLEN256
06. intmain()
07. {
08. charstr\[MAX];
09. intlen;
10. intnumber;
11. inti,j;
12. scanf("%s",str);
13. len=strlen(str);
14. number=pow(2,len);
15. printf("Allstrlist\\n");
16. for(i=1;i&lt;number;i++)
17. {
18. for(j=0;j&lt;len;j++)
19. {
20. if((i&gt;&gt;j)&1)
21. {
22. putchar(str\[j]);
23. }
24. }
25. putchar('\\n');
26. }
27. }

![](http://img.my.csdn.net/uploads/201209/19/1348068730_6368.jpg)

 

当字符串重复的时候, 先排序nlogn，此时重复的字符会变成连续的，bcaeb 排序以后,abbce, 在用O(n)把排序数组去重，再用上面方法。

 

**6. Swap two nodes in list**

SWAP(node \*head, node \*first, node \*second)

这道题目 还要构建节点比较复杂，题目的意思 就是交换两个节点。

当然这道题目 需要指针操作。

不能进行值的交换，因为类型不明哦。

 

7**Find Longest Repeat SubString ,可以overlap，区分大小写**

这道题一看就是典型的后缀数组的题目

应该比较简单

我跑一遍试试。

 

**\[cpp]**[view plain](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "view plain")[copy](http://blog.csdn.net/hiphopmattshi/article/details/7998207 "copy")

 

01. #include"stdio.h"
02. #include"string.h"
03. #include"math.h"
04. #include"assert.h"
05. #defineMAX1024
06. #defineCHARLEN256
07. intPartion(char\*str\[MAX],ints,inte)
08. {
09. inti=s-1;
10. intj;
11. char\*p=str\[e];
12. char\*temp;
13. for(j=s;j&lt;e;j++)
14. {
15. if(strcmp(str\[j],p)&lt;0)
16. {
17. temp=str\[i+1];
18. str\[i+1]=str\[j];
19. str\[j]=temp;
20. i+=1;
21. }
22. }
23. temp=str\[i+1];
24. str\[i+1]=p;
25. str\[e]=temp;
26. returni+1;
27. }
28. voidquickSort(char\*str\[MAX],intstart,intend)
29. {
30. intq;
31. if(start&lt;end)
32. {
33. q=Partion(str,start,end);
34. quickSort(str,start,q-1);
35. quickSort(str,q+1,end);
36. }
37. }
38. intcomLen(char\*str1,char\*str2)
39. {
40. intcount=0;
41. assert(str1&&str2);
42. while(\*str1==\*str2)
43. {
44. if(\*str1=='\\0'||\*str2=='\\0')
45. break;
46. count++;
47. str1++;
48. str2++;
49. }
50. returncount;
51. }
52. intmain()
53. {
54. charstr\[MAX];
55. charanswer\[MAX];
56. char\*strPoint\[MAX];
57. inti,len;
58. intmaxLen,clen;
59. scanf("%s",str);
60. len=strlen(str);
61. for(i=0;i&lt;len;i++)
62. {
63. strPoint\[i]=str+i;
64. }
65. quickSort(strPoint,0,len-1);
66. maxLen=0;
67. for(i=0;i&lt;len-1;i++)
68. {
69. clen=comLen(strPoint\[i],strPoint\[i+1]);
70. if(clen&gt;maxLen)
71. {
72. strncpy(answer,strPoint\[i],clen);
73. answer\[clen]='\\0';
74. maxLen=clen;
75. }
76. }
77. printf("%s\\n",answer);
78. }

用快排+后缀数组来解决的，还好测试的比较顺利

 

![](http://img.my.csdn.net/uploads/201209/20/1348070778_7261.jpg)