算法題庫1

1. Google: Write a code to merge N sorted array.
 
2. Microsoft: Reverse linked list by block
    eg. block size is 3 and linked list is 1 2 3 4 5 6 7 8
          output is 3 2 1 6 5 4 8 7
 
3. Google/Tencent: 總共25匹馬,每次只能5匹一起跑,那麼最少跑幾次才能比出第1、2、3名?
    不得使用timer之類的東西,只能靠一個場上跑來分名次。
 
4. Anonymous: 求m個字符串中重複n次字符串的最長的那個
    eg. abcd, bcde, sbbcdd, vbcx
    m = 4, n = 3 output is bcd
 
5. Microsoft: Given a input string find the longest substring which appears more than once in the string?
 
6. Microsoft: 一個包含n個整數的一維數組,找出第k大的數(k<=n).
 
7. Anonymous: 設計一種數據結構,不用隊列/堆棧能夠層次遍歷一顆樹.
 
8. Microsoft: 給一個字符串,求出該字符串的最長迴文子串.
 
9. Microsoft: 求正數數組內和爲指定數字的合併總數.
    eg. 5 5 10 2 3, 指定數字爲15
    15 = 5 + 10 = 5 + 10 = 5 + 5 + 2 + 3 = 10 + 2 + 3
 
10. Google: Given n lines in a panel, find how many intersection points are there(count in the duplicated intersection point).
 
11. Google/Microsoft: realize the function of random shuffle.
 
12. Google: 有一個無限輸入流,(假設空間不成問題)給一個解決方案,使得能在任何一個時間點上都能取得當前的Median.
 
13. Microsoft: 給兩個數組,分別存儲的是對一棵樹的前序遍歷和中序遍歷,根據這兩個數組構建出這棵二叉樹.
 
14. Google: 包含一個或多個{2,3,5}因子的叫醜數,比如2,4,5,15都是,17不是,找出第n個醜數.
 
15. Google: 給100萬個數(32位整型),設計一個方案來計算着100萬個數含二進制1的個數,綜合時間和空間複雜度的考量.
 
16. Google/Baidu: Find pervert pairs
      eg. 1 2 5 3 4 7 8 6
      pervert pairs: <5,3> <5,4> <7,6> <8,6>
 
17. Google: 有一個M*N個格子組成的房間,一個機器人要從房間的左上角走到右下腳,機器人每次只能右移一格或下移一格,求機器人一共有多少種走法,給出程序.
 
18. Microsoft: Implement Stack with Push() Pop() and Mininum() operation in O(1).
 
19. Google: Given an array a[n], build another array b[n], b[i] = a[0]*a[1]*...*a[n-1]/a[i], no division can be used, O(n) time complexity.

20. Amazon: find BST of max size in a binary tree (should be a sub tree).

21. Microsoft: How do you implement a linked list without using dynamic memory allocation? use an array as a linked list.

22. Yahoo: 找出最長0,1對數相同的子串
      eg. 01100111 --> 011001    110000011 --> 1100 / 0011

23. Yahoo: given a word,convert it into a pallindrome with minimum addition of letters to it. Letters can be added anywhere in the word.
      if yahoo is given result should be yahoohay.

24. Amazon: Suppose you have a file on disk which does not fit in main memory, how will you sort it? 
      Optimizing time and the number of reads/writes. Mention exactly how many reads/writes you would need.

25. Google: given a 3 number discrete random number generator, how can you design a 5 number random generator? 
      What is the average number of trials necessary?

26. Google: 兩個排序數組中求第k大的sum(a+b).

27. Amazon: 求一個數組的最大遞增子序列(不要求連續).

28. Bloomberg: Write an algorithm to find the number of six digit numbers where the sum of the first three digits is equal to the sum of the last three digits.

29. Microsoft: 二維平面上給出一個多邊形,按點的順時針順序表示,多邊形可以是凸多邊形也可以是凹多邊形。 給出一個點,判斷這個點是否在這個多邊形內部.

30. Microsoft: 判斷二叉樹B是否爲二叉樹A的子結構,實現判斷函數.

31. Google: 兩個大小相同的排好序的數組,A和B,如何找出兩個數組的中數.

32. Microsoft: 兩個單項鍊表彼此相交,求他們第一個交節點.

33. Google:  在一個字符串中找到第一個只出現一次的字符。如輸入abaccdeff,則輸出b.

34. Microsoft: 找出帶環單鏈表的環起始節點.

35. Anonymous: 以排好序的數組,找出數組最長等差數列的長度
      eg. 1 3 5 6 8 9 10 12 13 14
      最長等差數列爲6 8 10 12 14

36. Google: Given an array of elements find the largest possible number that can be formed by using the elements of the array.
      eg. 10 9->910    2 3 5 78->78532

37. Google: 兩個數組,就地(不用額外空間)找出合併後第k大的數.

38. Microsoft: Write a function to validate a BST.

39. Facebook: Given an array of size n, find all the possible sub set of the array of size k(all the subsets must be of size k).

40. Microsoft: Given a BST, print all nodes lying between two values, inclusive of these values. Write test cases for same.

41. Google: 二維平面上有若干點,求出一條直線能穿越最多的點.

42. Microsoft: 找出BST上任意節點的下一個節點.
      NODE* Next(NODE* pRoot, NODE* pToFind)

43. Google: 給定一個未排序數組,找出其中最長的等差數列. O(N*N)

44. Google: 在一個數組中尋找三個數,使得它們的和爲0.

45. Amazon: 最簡捷的方法判斷一個數是否爲2的冪.

46. Google:  兩個排好序的數組A, B.和一個數c, 求a + b = c, a屬於A, b屬於B.

47. Google: given a number n... print a spiral matrix in O(1) space example if n=5 the op should be:
      25 24 23 22 21
      10 09 08 07 20
      11 02 01 06 19
      12 03 04 05 18
      13 14 15 16 17

48. Amazon: given an array with positive and negative numbers find the first continuous subarray that sums to 0.

49. Adobe: Given an array A[i..j] find out maximum j-i such that A[i]<a[j] in O(n) time.

50. Facebook: 寫一個自己的 double sqrt(double x), 滿足一定精度就可以了.

51. Facebook: 反轉單向鏈表.

52. Facebook: Write a Program to rotate a N * M matrix by 90 degrees.

53. Anonymous: 對於正數數組A[n], 找出A[i] = A[x]+A[y]的A[i]的最大值.

54. Anonymous: 有一個元素個數爲2n的數組a,找出這樣數量相等(均爲n個元素)的兩個子數組a1,a2, 使a1中所有元素的和sum1與a2中所有元素的和sum2的差值最小,即|sum1-sum2|最小.

55. Google: 給你一個32位的number,把它按位逆序 : 1101001 ==> 1001011.

56. Google: 一個range的序列,如[1,3], [2,6], [8,10],[15,18],寫程序合併有重疊的range,比如上面的序列合併爲[1,6], [8,10], [15,18].

57. Amazon: 將字符串abcde12345 inplace的轉換爲a1b2c3d4e5.

58. Google: 給一個整數,表示成n個整數的平方和,n最小, 求n.
      eg. 10017 = 100^2 + 4^2 + 1    n = 3

59. Facebook: 實現atoi函數. 15min

60. Facebook: Implement division without using multiplication or division. It should work most efficient and fast.

61. Morgan Stanley: how to sort 100 numbers given using memory size sufficient for 20 elements only(like array of size 20)?

62. Facebook: How will you implement your own rand() such that it returns an integer from 0 to n-1 with equal probability?

63. Linkedin: Write a function that would return the 5th element from the tail (or end) of a singly linked list of integers, in one pass, and then provide a set of test cases against that function (please do not use any list manipulation functions that you do not implement yourself).

64. Amazon: Design an OO parking lot.What classes and functions will it have. It should say, full, empty and also be able to find spot for Valet parking.The lot has 3 different types of parking: regular, handicapped and compact.

65. Amazon: Given 2 rectangles with top left and bottom right coordinates for each rectangle, return 1 if they have a common area otherwise return -1.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章