算法类面试题-5

来源:互联网 发布:创业软件经营状态 编辑:程序博客网 时间:2024/05/22 12:28

121. [Apple] You are given a deck containing n cards.  While holding the deck:
1. Take the top card off the deck and set it on the table
2. Take the next card off the top and put it on the bottom of the deck in your hand.
3. Continue steps 1 and 2 until all cards are on the table.  This is around.
4. Pick up the deck from the table and repeat steps 1-3 until the deck is in the original order.
Write a program to determine how many rounds it will take to put a deck backinto the original order.  This will involve creating a data structure to represent the order of the cards. This program should be written in Python.  It should take a number of cards
in the deck as a command line argument and write the result to stdout.

    略。。。

 

122. Suppose there is a binary tree having millions of nodes and by mistake one node has two indegree (i.e. There becomes a cycle/loop in that tree. Tou have to find that node which is having two indegree) Constraint is no extra memory can be used and tree representation is in Linked list format.

???可能吗?没有额外memory, 否则做个DFS/BFS即可

 

123. Print the nodes on the exterior of a binary tree in a anti-clockwise order, i.e., nodes on left edge, then leaf nodes, then nodes on right edge.

    先按层遍历,将每一层的开始放到left edge, 结尾放到right edge. 再中序遍历打出所有的leaf node(这里取决于leaf node的定义), CODE取自:

http://www.leetcode.com/2010/10/print-edge-nodes-boundary-of-binary.html

124. 已知整数 m ,其二进制形式有 k 位为1,打印出 0≤x≤m 所有满足以下条件的 x
x^(m-x) = m,其中^是异或运算符。在 0≤m<2^n 范围内对每一个 m ,打印出所有的 x ,并求总复杂度。

TO LEARN, 像数学题

 

125. You can win three kinds of basketball points, 1 point, 2 points, and 3 points. Given a total score X, print out all the combination to compose X. (recursion/ Dp)

略。。。

 

126. 有n个job,分配给3个打印机,求所有任务完成的最短时间。例如:3,5,4每个打印机占1个job,最短时间是5. 15,7,8,10, 4, 15给1号打印机,7,8给2号打印机,10,4给3号打印机,最短时间是15.

http://en.wikipedia.org/wiki/Partition_problem

    见133题

 

127. 设计一个算法,判断一个integer n是不是可以表示成k(k>=2)个连续正整数的和

    假设所要分解的数为x ,分解成n个数,那么我们可以这样表示:

x=m+(m+1)+(m+2)+……….+(m+n-1)

其中m为分解成的连续整数中最小的那一个,并且我们知道m大于等于1的正整数。易知:

x=(2m+n-1)*n/2, 变换一下的m=(2*x/n-n+1)/2

由m的范围我们知道(2*x/n-n+1)/2>=1 以上就是x和n的关系。给定一个n看是否x能分解成n个连续整数的和可以判断是否存在m,也就是转换成(2*x/n-n+1)是否是偶数。

代码略

 

128. how to serialize and deserialize a n ary tree?

见79题

 

129. 如何刷屏最快

    G(n) = max{G(n-1)+1, g(n-k)*(k-2)}

 

130. Given a sorted array with duplicates and a number, find the range in the form of (startIndex, endIndex) of that number. For example, given 0 2 3 3 3 10 10 and 3, return (2,4). Given the same array and 6, return (-1,-1).

    Binary Search的扩展,见142题

 

131. 有N个整数,M个bucket,每个bucket都可以装至少N个整数,一个bucket的value等于放入它的所有整数的和。现求解一种分配方法,使之 minimize(the max value of all buckets)

NP完全问题,见:

http://en.wikipedia.org/wiki/Job_shop_scheduling

 

132. Given pairs of connected items ((A, B), (B, C), (C, D)…), find the root
node of this tree.

    找到入度为零的node即可

 

133. There’s a book and each sentence consists of several words. How to find the minimal set of words which can used by the reader to understand half of total number of the sentences. Each sentence is readable if the reader knows all the words.

TO LEARN, 有点类似于dancing links用到的满足问题

 

134. [facebook]求一段时间内出现最多的ip address(es)

    只能是搞几张aggregate表,按秒,分钟,小时等做为粒度。。。

 

135. 两个文件里面存着行程安排,开始时间以及结束时间,设计算法找所有conflicts。

    如果要找所有conflicts, 则复杂度为O(N^2), 略。。。

 

136. what’s the best data structure for storing and looking up a huge amount of url’s (presented in a prefix format) like these:
com.google.www -> value1
com.yahoo.finance -> value2
com.yahoo.finance/stocks -> value3
com.yahoo/finance -> value2
1.2.3.4/node/123 -> value4
….
the requirements are:
1.  it has to be compact (compression if necessary).
2.  lookup time should be fast (constant would be ideal, but a few level of tree lookup is fine too).

    有点类似于设计题,可以用一个优化过的prefix tree?

 

137. Subset sum problem

可以转换为背包问题

   

138. Dutch National Flag Problem (DNFP)

http://www.iis.sinica.edu.tw/~scm/ncs/2010/10/dutch-national-fl

    如果有多于三种元素,则称为America Flag Problem, 本质上是radix sort

139. 一个单词的列表,要找出两个单词,它们没有相同的字母出现,而且长度乘积最大

    难题, TO LEARN

 

140. You are given N blocks of height 1…N. In how many ways can you arrange these blocks in a row such that when viewed from left you see only L blocks (rest are hidden by taller blocks) and when seen from right you see only R blocks? Example given N=3, L=2, R=1 there is only one arrangement {2, 1, 3} while for N=3, L=2, R=2 there are two ways {1, 3, 2} and {2, 3, 1}.

    DP题,定义g(n,l,r)为题目的答案,而f(n,l)为n个block,从左边看到l个block,则递推公式为:

g(n,l,r) = (1<=k<=n)sum(C(n-1,k-1)*f(k-1,l-1)*f(n-k,r-1))

f(n,l) = (1<=k<=n)sum(C(n-1,k-1)*f(K-1,l-1)*(n-k)!)

f(n,1) = (n-1)!

F(n,n) = 1

F(n,m) = 0 if n < m

 

141. There is a binary tree(Not a BST) in which you are given three nodes x,y,z. Write a function which finds whether y lies in the path b/w x

    如何定义in the path?…略。。。

 

142. binary search的各种变种

1)如果找不到元素,返回应该插入的位置

2)如果数组允许有重复,寻找最小的那个i,使得arr[i] = v, (第一次出现的位置)

3)如果数组允许有重复,寻找最大的那个i,使得arr[i] = v

与2)类似
4)如果数组允许有重复,寻找最小的那个i,使得arr[i] > v

5)如果数组允许有重复,寻找最大的那个i,使得arr[i] < v

与4)类似
6)给2个有序数组,大小一致,如何求他们的median

见第68题
7)循环数组的二分查找

    143. 怎样de-dup一个sorted array?

可以直接线性扫描,也可以用binary search来优化(如果重复数比较多)

   follow-up: 如果有一个big single file, many servers, how to use these servers to compute this problem? 要求尽量balance load

    balance load,可以将big file分成比较小的块,每台机器完成若干块,根据计算情况进行调度。

 

144. Given a number n, find the nearest palindrome of n just greater than n.

算法:

  1. 看左边一半的反是不是等于右边一半,如是,直接返回
  2. 如果不是,看左边一半的反是不是大于右边一半,如果是,将右边一半改成左边一半的反,再返回
  3. 否则,将左边一半的最后一位加1,再重复第二步(这次不用考虑大小)

CODE

 

145. 有一个不定长的char string (S1),都是0,1组成;另外一个string (S2), 也是0,1
组成,可以认为是pattern,问S1中是否含有S2
e.g S1 = 11100011 10101011 11111111 10000100 ( 4 bytes, ’11100011′ is 1 byte)
S2 = 00111010
the answer is ture, 1110″00111010″1011

http://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_string_search_a

    Rabin Karp算法 ? 略。。。

 

146. Write a function for a linked list that exchanges the positions of the nodes after the nodes referenced by two given pointers t and u. (assume t and u are either in the list or NULL).
Example:
1 2 4 3 6 8 5 7 9 10
Exchange nodes after 2 and 3:
1 2 6 3 4 8 5 7 9 10

    简单题,略。。。

 

147. Design a data structure for a text editor. Require better than linear for both line search and insert a new line.

    Notepad++使用gap buffer, 即在一段buffer的中间留下空间,这样插入和删除操作都是O(1). 关于search,可以用Rabin karp算法。

Rope 也是一种可以考虑的数据结构,见:

http://en.wikipedia.org/wiki/Rope_%28computer_science%29

 

148. Given an array of 0′s and 1′s. only, find the maximum length of the subarray such that the number of 0′s and 1′s in that subarray are equal.

将所有的0换成-1,先计算累加和,cum[i] = a[0]+…+a[i], 则问题转化为:求cum[i]相同时的最大的index差。扫描计算即可,复杂度O(N)

类似的问题有(利用累加和):

1.一个有正有负的数组中,求一个subarray使其和最接近0

2.对数组进行add(l,r,v)操作,即对a[l]到a[r]的每一个元素加上v, 现有N个这种操作,怎么在O(N)时间内完成?

 

149. 一个数组有很多key,已知所有key属于三个组,存在先后顺序,现在要求把所有key按照组排序,比如{1, 2, 3}, {4, 5}, {6, 7},key之间不需要有顺序,只要不同组之间的元素在数组里满足组规定的顺序就行了(DNFP?)

DNFP问题,见138题。

 

150. 游戏算法,给定一个N*N格的板块,往上面放不规则的element。
如何表达这个板块,用什么数据结构来表达element,这个element有可能是任何形状,like “—”,X”,“Y”,“田”(参考俄罗斯方块)
如何判断这个element可以放在某个cell里面,放在cell的条件是这个element可以覆盖这个cell。(element之间不能重叠)
象俄罗斯方块一样,这个element可以rotate四个角度,问如何判断rotate后可以放入。

    TO LEARN, 俄罗斯方块?

    这里的描述不是很清楚,如果是俄罗斯方块的话:(取自http://wintris.codeplex.com/

原创粉丝点击