C++编程,数据结构,算法类面试题集.(3)

http://www.mianwww.com/html/2013/03/17814.html

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,再重复第二步(这次不用考虑大小)
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/)

 

151. 给你一串数字,让你写程序,把操作符(可以是任意多个 + – * / %)放进各个数字之间,同时还可以在任意位置放进括号,让这个算术表达式的值等于一个给定的数字。比如:给你 5 3 8 9 = 6你的程序应该输出 5 * (4 + 8) % 9 = 6

    CODE, 24点

    152. 一道编程题,大意是给定一个类read1,它有一个函数read4096,每次调用它可以从文件中读取4K个字节,同时移动文件指针4K个位置(若文件中剩余数据不足4K,则读取剩下的所有数据),这个函数返回实际读取的字节数,int型;要求实现另一个类read2中的一个函数read,它有一个参数int n_byte,这个函数可以从文件中读取由n_byte指定的字节数,同样返回实际读取的字节数;然后又给出一个函数reset,它可以将文件指针重置到起始位置,要求实现read2中的另一个函数seek,有一个参数int pos
,它可以将缓冲区的指针移动到第pos个字节的位置,返回实际指针移动到的位置。可以在read2中添加任意变量来完成这两个函数。

    多次做read4096即可,为了加速,可以重用上次seek的结果

 

153. 编程题问的是boggle游戏的问题:给定一个4*4的矩阵,每个位置有一个字母,可以从一个位置跳转到周围八个相邻位置中的任何一个,但不能跳到已经访问过的位置,要求找出所有的单词(假设给定了一个词典)。http://en.wikipedia.org/wiki/Boggle

构造一个单词的prefix字典,然后再递归+回溯。。。

 

154. Find median for k sorted arrays

设k个sorted array为a1, a2, …, ak.

先找出这k个sorted array的median, m1, m2, … mk.

再找出这k个median的median: mm

然后可以把所有比mm小的数和大的数都去掉,大致有一半

然后再找剩下的数的median(递归),复杂度O(klogn)

 

155. “Count and Say problem” Write a code to do following:
n String to print
0 1
1 1 1
2 2 1
3 1 2 1 1
…
Base case: n = 0 print “1″
for n = 1, look at previous string and write number of times a digit is seen and the digit itself. In this case, digit 1 is seen 1 time in a row… so print “1 1″
for n = 2, digit 1 is seen two times in a row, so print “2 1″
for n = 3, digit 2 is seen 1 time and then digit 1 is seen 1 so print “1 2 1 1″
for n = 4, you will print “1 1 1 2 2 1″

Consider the numbers as integers for simplicity. e.g. if previous string is “10 1″ then the next will be “1 10 1 1″ and the next one will be “1 1 1 10 2 1″ 10

    CODE

 

156. 给定一个数组, A[ 0 .... N ]作为输入数组。给定一个函数 f(i,j) ,这个函数以两个下表(i,j)为输入, 返回一个值。(这个函数是个blackbox, 唯一的信息就是输入两个整数返回一个值)。要求把数组A分为3份,使得 f(0,a) + f(a,b) + f(b,N)最小。
LEARN

 

157. 给n*m的字符矩阵。然后给你一个字符串。问是否可以在矩阵中找到他的track。track是指从其
中一个符出发,可以向四周走,可以重复,可以回头。
e.g:
a b
c d
string:  ‘bdba’ could be found but not for ‘bcd’.

递归+回溯,也可以用DP优化

 

158. Given three linked list of integers, all sorted. Find the first shared integer in all three. What is the time complexity?

    略。。。

 

159. Initially you have a 2×2 matrix, say zoom1:
a b
c d
zooming it results in a 4×4 matrix (zoom2) as follows:
aa ab ba bb
ac ad bc bd
ca cb da db
cc cd dc dd
zooming it again will result in an 8×8 matrix and so on..
aaaa aaab abaa abab baba babb bbba bbbb
aaac aaad abac abad babc babd bdba bdbb
acaa acab adaa adab bcba bcbb bdba bdbb
acac acad adac adad bcbc bcbd bdbc bdbd
caca cacb cbca cbcb dada dadb dbda dbda
cacc cacd cbcc cbcd dadc dadd dbdc dbdd
ccca cccb cdca cdcb dcda dcdb ddda dddb
cccc cccd cdcc cdcd dcdc dcdd dddc dddd
The question is, given a sequence say abaaccda… we need to find out the
sequence coming just left to it. For e.g. if the given sequence is “bd” for
zoom2, the sequence coming just left to it is “bc”. For “cd” it’s “cc” etc.

    CODE

 

160. 如何在binary tree找一个path 从root 到leaf, 和是sum?
2)  如何序列化一个binary tree到一个文件
3)  如果有一个已经序列化的tree, 很大,要做1)的算法,怎么做,2)中如果有多个方法选择哪中序列化的方法比较好?
4) 如果有1000w个已经序列化的小文件,对他们都要做3),如何提高性能,系统是5台机器

    TO LEARN

 

161. Programming: interval halving. Given a continuous function ‘f(x)’ and an interval on the x-axis from ‘start’ to ‘end’. It is know that ‘f(x)=0′ for exactly one value of ‘x’ between ‘start’ and ‘end’, and that ‘f(x)’ crosses the x-axis at this point. Write a program that repeatedly cuts in half the interval until the interval containing ‘f(x)=0′ is equal or less than ‘epsilon’ wide.

    略。。。

 

162 [Facebook] You are given N ranges of date offsets when N employees are present in an organization. Something like
1-4 (i.e. employee will come on 1st, 2nd, 3rd and 4th day )
2-6
8-9
..
1-14
You have to organize an event on minimum number of days such that each employee can attend the event at least twice. Write an algorithm (there is apparently an O(n) algorithm for this).

    先按结束的时候排序,然后依次选取

 

162. Search in skip list

    TO LEARN

 

163. 给定一个string,可以任意删除其中的char,以使的剩下的string成为palindrome,求最长的这样的palindrome。问有啥dp算法可以解?

    与reverse求longest common subsequence

 

164. 把一个有大写字母和小写字母的字符串变换成小写字母在前面大写字母在后面的字符串

    略, partition

 

165. 给很多date ranges,一个array, 每个date range有开始日期和结束日期,判断连续不连续

    CODE

 

166. 实现hash表

    CODE

 

167. 判断两二叉树全等(在可以交换左右子树的条件下),进一步给出需要多少次交换。

    CODE

 

168. 一个NxN矩阵,每个格子有一个整型数,从左上角到右下角找一条路径使得经过的格子数字和最大。只能向右和下移动。时间复杂度,如何优化。  

    DP,复杂度O(N*N)

 

169. 现在假设有一堆整数数组,有一个flip函数,接受一个数组下标i为参数,作用是将数组index 从0到i的元素反转。eg. 假设数组为5, 6, -1, 3, 2, 如果调用flip(3),那么就将数组下标0, 1, 2, 3反转,数组变为 3, -1, 6, 5, 2。问:只使用flip函数(不能直接用swap或数组下标操作[]等对数组进行修改),来对该数组排序。

    IDEA:每做两次flip将当前最大的item放到位上

    CODE

 

170. 一个大小为N的数组,其中有N-1个不同的整数,范围从0-N, 问找出missing的那个
整数。然后又扩展,问如果有k个missing,如果用O(1)space去找出来。

    IDEA:将整数i放到数的第i位上

    CODE

 

171. Suppose we have a stream of web clicks. Now you need to provide at any time the count of web clicks during the past 1 minute.  To be specific, you get informed whenever a web click happens. You need to provide a function “getCount()” such that once called, return the count of clicks during the past 1 minute. The solution will be both constant in time and space.

    用一个circular buffer来保存每一秒的click数,再维护一个total数即可。

 

172. 一个有序序列,从某个地方rotate,求在rotate的位置,比如 1 3 5 0 0 0,那么rotate的位置是5,他后来只用了5行就写出来了,很nb,被bs了~~
173. 二分图最大匹配

    略。。。

 

174. [Facebook] implement copy-on-write string class.

    TODO

 

175. 给你n个数字a1,a2…an,表示坐标上面(i,ai)个点,i=1..n(i,ai)到(i,0)共n个线段,从中挑两条,和x轴一起构成一个容器,让容器能装的液体容量最大(容器不能倾斜)。

    穷举?。。。

 

176. Describe an algorithm that takes an unsorted array of axis‐aligned rectangles and
returns any pair of rectangles that overlaps, if there is such a pair. Axis-aligned means that all the rectangle sides are either parallel or perpendicular to the x‐ and y‐axis. You can assume that each rectangle object has two variables in it: the x‐y coordinates of the upper‐left corner and the bottom‐right corner.

    穷举?。。。

 

177. Given a list of intervals, 比如 (10,20),(30,50)…,and a target interval,
比如(18,35) 找有多少overlap.

http://programmers.stackexchange.com/questions/64132/interestin

    INTERVAL TREE

 

178. 给定一个integer array, a1,a2…an, 找出所有a,b,c,d使得a+b = c+d. 很容易找到O(n^2)空间,O(n^2)时间的算法,不知道有没有更快更好的。

    如果所有的数都相等,那至少需要O(n^2)时间复杂度。另外排序后,二重循环加两头扫描,不需要额外的空间复杂度了。

 

179. n个球, n很大 > 1 billion, k个颜色, k相对小, 比如10. 要求in space sort最efficient的做法 白板coding. (hint, k<< n 所以最后算法争取比nlog(n)小)
该题的第二问 k = 1000 实现比nlogn 更efficient的in space的算法

    见138题

 

180. If the Fibonacci series is 1,2,3,5,8,13,….. then 10 can be written as 8 +
2 ==> 10010 and 17 can be written as 13 + 3 + 1 ==> 100101. The Question was, given n, I need to get all possible representations of n in Fibonacci Binary Number System. as 10 = 8 + 2 ==> 10010 also 10 = 5 + 3 + 2 ==> 1110 (Zeckendorf’s theorem)

如果要得到所有的组合,先计算出该数范围内的Fibonacci数,再当成找零问题来计算就可以了。

Zeckendorf’s theorem:

Every positive integer can be represented uniquely as the sum of one or more distinct Fibonacci numbers in such away that the sum does not include any two consecutive Fibonacci numbers.

 

181. void reversePairsOfLinkList(Node*& head) {}
[] => []
[A,B] => [B, A]
[A, B, C] => [B, A, C]
[A, B, C, D, E, F, G]  =>  [B, A, D, C, F, E, G]

    见195题

 

182. You have to paint N boards of length {B1, B2, B3… BN}. There are K painters available and you are also given how much time a painter takes to paint 1 unit of board. You have to get this job done as soon as possible under the constraints that any painter will only paint continuous sections of board, say board {2, 3, 4} or only board {1} or nothing but not board {2, 4, 5}.
know it could be solved by DP. But solution space seems quite big. What is the optimal solution? Thx.

    DP,类似于103题

 

183. 一个range的序列(链表或数组),如[1,3], [2,6], [8,10],[15,18] 写程序合并有重叠的range,比如上面的序列合并为[1,6], [8,10], [15,18] 如果这个序列不是静态的,而是一个数据流,如何 处理?
    INTERVAL TREE

 

184. 实现atoi,要考虑特殊的情况,比如不合法的输入等等。参照这个定义
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

   

185. word edit distance.

    伪代码如下:

   

 

186. longest palindrome substring.

http://www.akalin.cx/2007/11/28/finding-the-longest-palindromic-substring-in-linear-time/

    如果是substring,那可以按中心位置依次搜索。

    如果是subsequence, 与它的反求longest common subsequence即可

 

187. Describe and analyze an algorithm to find the longest prefix of a given string that is also a palindrome. For example, the longest palindrome prefix of ILLINOISURBANACHAMPAIGN is ILLI, and the longest palindrome prefix of HYAKUGOJYUUICHI is the single letter S. For full credit, your algorithm should run in O(n) time.

    TO LEARN, 后缀树?

 

188. 给一个数据结构数组,(parent, child), 重建二叉树,总是先遇见leftchild, 再遇见right child,假设输入没有问题。要求返回root。

    用一个hashtable来保存所有见到过的结点。 Root结点就是没有在child位置上出现过的结点。

 

189. 1.    两个sorted array, 如果merge成一个array。
2.    如果这两个array没有sort呢?并分析复杂度。
3.    如果有K个没有sorted的array怎么办呢?
4.    如果当前机器有K个cpu, 怎么处理问题3呢?复杂度分析。(考虑multithreading)

    后面几问假设是merge成一个sorted array, 那这个题就类似于外部排序的多路归并。

 

190. 给定一个tree, 每个节点有一个数值, 如果找到一个从root到leaf的path,使得这个path上的所有节点的sum最小。 Interviewer所要的答案是和hashtable联系上的,因为考虑到树很大的时候需要很长的时间。这个题很容易用recursive的方式解答,可是这个不是interviewer所要的答案。后来按照interviewer的意见,还是基本写出了用hashtable的算法。

    不理解题目的意思。。

 

191. 给定一个没有通往父节点的连接的BST, 找到大于x的最小的那个节点

中序遍历。略。。

 

192. 技术问题是找一个binary tree的叶子的最少depth

    分层遍历即可,略。。

 

193. Integer to Roman number

    CODE

 

194. 有一行animal cages,每个cage的动物的用水量为数组,有两个pipe给所有动物供水,pipe给当前cage的cost 是 这个cage动物的用水量,给其他cage的动物供水的cost是(distance to that cage)*那个cage动物的用水量, 求两个pipe供水的位置使cost最小。

    TO LEARN

 

195. 问把整数分成 sum of square的经典问题

    TO LEARN,数论题。。。

 

196. longest increase consecutive subsequence. e.g. 3, 2, 4, 5, 1, 6. Return {2, 4, 5}

    CODE

 

197. 用1*2的瓷砖覆盖8*8的地板,有多少种方式呢?如果是N*M的地板呢?
    中等难度的DP,有个解题报告见:

http://www.cnblogs.com/PureMilk/archive/2008/07/21/1247492.html

公式的论文见:

http://arxiv.org/abs/math/0310326

 

198. 生成Dyck word list

    与生成所有合法的括号组合类似,见:

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

 

199. one integer array A with ascending order, for example  1 ,2 ,3,4, please generate another array  B with any sum of any 3  different numbers picked from the A with ascending order, for example for 1,2,3,4, the result is (1+2+3),(1+2+4),(1+3+4),(2+3+4)“
    三重循环即可,代码略。。

 

200. int array a[n] with all element 0<=a[i]<=1000,find the subarray with the largest sum that is <= max and output the starting and ending index of this subarray and the sum. If there is more than one such subarray, output the one with smallest starting index.

算法:

先求出累加和cum[i] = a[0]+…+a[i],从a[k]>max开始,在前面二分查找第一个cum[l]>=(a[k]-max), 直到找到范围最大的range. 复杂度O(NlogN)

 

201. given is an unsorted integer array, how to divide it into two subarrays, such that the averages of these two arrays are the same (or have minimum difference).what if those are positive integers only, and what happens when it is mixed with positive and negative integers?

    线性扫描并计算一下即可。正负数是否有关系?

 

202. 一个arraylist,里面有很多string,它们按照未知的alphabetical顺序排列,要求输出所有可能的alphabetical order,(后来又问了如何判断是否有conflict)
例子:it is a good day today,
it<is  –  t<s
is<a   –  i<a
a<good –  a<g
good<day–g<d
day<today–d<t

两两比较找第一个不同即可。

判断是否有冲突?整个关系形成一个图,这个图应该是无环的,做一次DFS即可。

 

203. 有一堆的工作,每个工作有开始和结束时间,假设表示为[2,3],[3,7]等等,现在要求在[1,n]的时间段内选出尽可能多的工作.

    贪心,尽量选择结束时间最早的工作即可

 

204. 给一个数组 里边都是整数 求最大乘积的连续子数组

先按零把数组分成若干个子数组,再在子数组里找包含偶数个负数的最大的范围。

 

205. f(i,j)=2^i*5^j给出一个长度为N的(i,j)序列,使得f值递增,i>=0,j>=0
设f(i,j)是第k个数f_k, 则下一个数取使i+1或者j+1中间比较小的那个即可

    206. f(N): return round(reduce(lambda x,y: int(x)+int(y), list(str(N))))/len(N) N为整数,f函数返回N各位数值的平均数,现在给出一个正整数范围[begin, end],要求得出该范围中符合f(N)>=7的数的集合,希望算法尽可能比end-begin+1次test快。

    TO LEARN,

 

 

207. There is a straight road with ‘n’ number of milestones. You are given an array with the distance between all the pairs of milestones in some random order. Find the position of milestones.
Example:
Consider a road with 4 milestones(a,b,c,d) :
a <— 3Km —>b<— 5Km —>c<— 2Km —>d
Distance between a and b = 3
Distance between a and c = 8
Distance between a and d = 10
Distance between b and c = 5
Distance between b and d = 7
Distance between c and d = 2
All the above values are given in a random order say 7, 10, 5, 2, 8, 3.
The output must be 3,5,2 or 2,5,3

    对这个数组里的每一个数,如果它不能表示为该数组里两个数之和,则是一个要求的两个milestone之间的距离

 

208. 找二叉树两个最大的相同子树

    不是很理解题意。。。两两比较即可,复杂度O(N^2),如要加速,可以先计算每个结点的深度和子结点数,相同再进行比较。

 

209. Given an array of elements find the largest possible number that can be formed by using the elements of the array.
eg: 10 9
ans: 910
2 3 5 78
ans: 78532
100 9
ans: 9100

先按如下规定排序: a > b if ab > ba,

然后再从高到低拼接起来即可。

 

210. You have an array of 0s and 1s and you want to output all the intervals (i, j) where the number of 0s and numbers of 1s are equal. Example index = 0 1 2 3 4 5 6 7 8 array = 0 1 0 0 1 1 1 1 0 One interval is (0, 1) because there the number of 0 and 1 are equal. There are many other intervals, find all of them in linear time. How can this be done in O(n)?? find all intervals, not find the longest interval.

    O(N)好像不可能做到,因为所有的intervals可能就是O(N^2)级别的

-

211. Rabin Karp Algorithm

    略。。。

 

212. Given a list of presentations with begin and end time that all need to use a conference room. We want to optimize the utilization of the room by allowing maximum hours of the conference room being used.

DP题,先把所有的presentation按endtime排序,然后设q(endtime)为在endtime之间conference room被使用的最长时间。然后依次用presentation与之间所有的q相比,得到新的q.总体复杂度O(N^2)

 

213. Given an array of int, each int appears exactly TWICE in the array. Find and return the int such that this pair of int has the max distance between each other in this array.
e.g. [2, 1, 1, 3, 2, 3]
2: d = 5-1 = 4;
1: d = 3-2 = 1;
3: d = 6-4 = 2;
return 2

用hashtable保存num->index,然后线性扫描一遍即可

214. 二进制加法
/**
* i.e.
*
* char a[] = “11″;
* char b[] =  “1″;
* char *c = bstradd(a, b); // c is a pointer to “100″
**/

215. 给你一列单词/字符串(内部字符范围:unicode),例如:banana, cat, dog, elephant, type, middle, lake, 让你把这些单词排列成任意相邻单词不能有任何相同字符的序列,如果确定无法满足这个要求,返回false.

考虑一个图,满足的首尾字母不同即有一条a->b的边,那该问题等价于找Hamilton圈,是一个NP完全问题

 

216. 判断(二叉)树是否镜像对称

    镜像对称的定义?

 

217. Given a binary tree, find 2 leaf nodes say X and Y such that F(X,Y) is maximum where F(X,Y) = sum of nodes in the path from root to X + sum of nodes in the path from root to Y – sum of nodes in the common path from root to first common ancestor of the Nodes X and Y

除了穷举,没啥好idea… 求所有的和O(N),求最近公共父结点可以优化到O(1)具体比较为O(N^2),总体复杂度为O(N^2).

 

218. x^n = y, x/n/y都是整数,n>1,y叫做一个啥数来着,姑且叫做Super Cool数吧,
比如,1^2 = 1×1=1, 1^3 = 1×1×1 = 1 …
2^2 = 2×2=4, 2^3 = 2×2×2 = 8 …
现在给你一个整数y,请返回最近的那个Super Cool数,写Code。

对每一个小于sqrt(y)的数,求对数后找一个最接近的即可。。

 

219. 题目是给定一个unsorted int array, 怎么找到第一个大于0,并且不在此array的integer。
比如[1,2,0] return 3, [3,4,-1,1] return 2. (time O(n), constant space?)

    将第i个数放到i位,再从头扫描

    220. 给定一个数字数组 (Let’s call it count-array) ,其中每个元素是从末端数小于原数组中该元素的个数。求原数组。原数组中元素是从1到n。
Example:
原数组  4,1, 3, 2
Count array  3, 0, 1, 0
求nlogn的算法。

    CODE,考虑通过merge sort的变形

 

221将整型变量 x 中数字左右翻转后存到另外一个整型变量 y中,例如 x = 12345 时,y为 54321,x = ‐123 时,y为‐321。其中 x 的个位不为 0。      void reverse (int x, int* y); 

    CODE

 

222. 对集合{1, 2, 3, …, n}中的数进行全排列,可以得到 n!个不同的排列方式。现在我们用字母序把它们列出来,并一一标上序号,如当 n=3 时:
0.123
1.132
2.213
3.231
4.312
5.321
现在,请书写一个函数 void print (int n, int k), (函数原型是用 C语言写的,你可以用你熟悉的语言)在已知 n和序号 k 的情况下,输出对应的排列,并简要阐述思路

    223. 一维数轴上有 n 条线段,它们的端点都是已知的。请设计一个算法,计算出这些线段的并集在数轴上所覆盖的长度,并分析时间复杂度。例如,线段 A 的坐标为[4, 8],线段 B 的坐标为[1, 5.1], 那么它们共同覆盖的长度为 7。 请尽量找出最优化的算法, 解释算法即可,不必写代码。

    TO LEARN, INTERVAL TREE

 

224. 3 sorted arrays, A, B, C, find indexes i, j, k, so that max(a-b, b-c, c-a) is minimized. (a = A[i], b = B[j], c = C[k]) Another version is to minimize max(|a-b|, |b-c|, |c-a|).

    IDEA:类似于三路归并,反复地将当前最小的元素往前移,并Update目标值即可。

 

225. 一条直线上有N个站台,已知任何两点间直达列车的票价,求出从起点到终点的票价最优的乘车方案。因为从A到B,再从B到C的价格可能比直接从A到C便宜

    DP。。。。

226. N个job,要求分配到M台机器上,每个机器可以被分配0-N个job,但有些job相互排斥不能被放到一起执行,给出所有可能的分配方案

要给出所有方案只能递归穷举吧

 

227. 给N个元素,第i个元素有一个大于0的score(i),要求随机选出k个,每个元素可以被选择任意多次,但保证被选择的概率要和score(i)成比例

    将0-1之间按scroe(i)的比例划分成区间,然后生成0-1之间的随机数,按其所在区间决定为哪个元素

 

228. N个矩形,所有矩形都有一条边在同一条直线上,他们相互可能有overlap,找出最后得到的这个不规则图形的所有边界点

TO LEARN,

也是INTERVAL TREE或者SEGMENT TREE?先取出所有的点,再把被任意矩形所包含的点去掉。

 

229. 给两颗树,如果节点深度相同且value相同,则这两个node是match的,两棵树上的节点如果相互match,则它们的父节点必须也要match。假设一棵树上所有node的value都不同,并且兄弟节点间不用考虑顺序,问给两棵树,如何求最大match的node数目。如果value有重复,并且要求兄弟节点match的顺序一致,问如何求最大match数。

    CODE

 

230. Write a program to find the largest possible rectangle of letters such that every row forms a word (reading left to right) and every column forms a word (reading top to bottom). Words should appear in this dictionary: WORD.LST (1.66MB). Heuristic solutions that may not always produce a provably optimal rectangle will be accepted: seek a reasonable tradeoff of efficiency and optimality. (Hint: Use a B-Tree)

http://www.itasoftware.com/careers/work-at-ita/hiring-puzzles.h

    TO LEARN, hard.

 

231. 直方图盛水

    CODE

 

232. We have been given a deck of cards and each combination has a rating eg 3 As is higher than 2 As 1K etc. Write an algorithm such that the probability of picking 3 or 5 or 7 cards from the deck results in high rating

    DP,写递推公式

 

233. 求一个unsorted数组中最长的等差数列(int 数组,可递增或者递减)
http://compgeom.cs.uiuc.edu/~jeffe/pubs/pdf/arith.pdf

    难题。。。略

 

Jump Game:
Given an array start from the first element and reach the last by jumping. The jump length can be at most the value at the current position in the array. Optimum result is when u reach the goal in minimum number of jumps.
For ex:
Given array A = {2,3,1,1,4}
possible ways to reach the end (index list)
i) 0,2,3,4 (jump 2 to index 2, then jump 1 to index 3 then 1 to index 4)
ii) 0,1,4 (jump 1 to index 1, then jump 3 to index 4)
Since second solution has only 2 jumps it is the optimum result.
要求:O(N) time, O(1) space.

类似于DP?反复update jump N+1步后,记录到达当前位置的最小步数


234.一个数组,不考虑重复数字,找出数组中有多少个cycle。cycle:每个element去找他对应的index,如果index在界内,就继续找这个index的值所对应的下一个index直到找到一个cycle,或者出界。
比方说 0,2,1,9,10
对于0, index 0 就是 val 0, 所以是一个cycle
对于2, index 2 是 1, 在界内,就找 index 1,which is 2,所以又是一个cycle
总共4个cycles: 0–>0, 2 -> 1, 9 -> 界外, 10->界外
先要求写了个可以有extra space的,然后要求no extra space

No extra space需要修改原数组吧。。。CODE

 

235. bool isOverlap(int a, int b, int c, int d)参数取值范围0~6代表星期几。能不能不用大于号和小于号

    用一个七个数的数组来标记就行了吧

 

236.给一个吸地毯的irobot,和一个长方形的屋子,四面有墙,四个指令:
Bool moveForward()//向前走一格,走不了的话返回false
Void Rotate(int degree)//就是左拐右拐
Bool isClean()//当前单元格是否干净
Void clean()
把irobot 扔在屋子任意位置,写代码让irobot清理房间,每一格都要走过(单元格没有坐标)

    先走到角落,然后来回扫就行了

 

237. Edit Distance

    见185题

 

238. {1,5, -5, -8,2,  -1,15 } 要把负的扫到左边,正的扫到后边。不能改变顺序得到{-5 -8 -1 1 5 2 15}  这个题有time 低于 n^2 space=O(1)的解法吗

应该没有这样的解?

 

239.给一个通常的表达式,转成后缀表达式

    CODE

 

240. Given n arrays, find n number such that sum of their differences is minimum. For e.g. if there are three arrays

A = {4, 10, 15, 20}
B = {1, 13, 29}
C = {5, 14, 28}
find three numbers a, b, c such that |a-b| + |b-c| + |c-a| is minimum. Here the answer is a = 15, b = 13, and c = 14

    同224题

 

241. reverse double linked list.

    CODE

 

242. [Facebook] Given an array A of positive integers. Convert it to a sorted array with
minimum cost. The only valid operation are:
1) Decrement with cost = 1
2) Delete an element completely from the array with cost = value of element

DP,写递推公式,使[0,i]中的元素有序,update时,加入[0,i+1],要么decrement以前的,要么删掉i+1

 

243. 在一个平面上有n个点,设计算法看能不能找出四个点构成一个正方形,分析时间复杂度。

    TO LEARN

 

244. Algorithm to find the two numbers whose difference is minimum among the set of numbers.
For example the sequence is 5, 13, 7, 0, 10, 20, 1, 15, 4, 19
The algorithm should return min diff = 20-19 = 1.
Constraint – Time Complexity O(N) & Space is not a constraint [upto O(3N)]
Assumption – Sorting O(nlogn) & comparison of adjacent numbers is already known & is not an option. Try to keep it linear

    想不出来。。。

 

245. Given an array of integers (both positive and negative) divide the array
into two parts (sub-arrays) such that the difference between the sum of
elements in each array is minimum?

如果subarray连续的话很简单。。。

 

246. 给一个string, 比如“facebook”, 可以拆成“face”和“book”, 对任一string, 找出最长的可以拆分成其他单词的子串。

    先在string里找是单词的范围,得到一组range,然后将这些range按起点排序,再递归+回溯,找到最长的能正好拼接在一起的最长的范围

 

247. 有10个unsorted array, 分给10太不同的机器处理,这10台机器之间不能通信,但可以和总机通信,如何求总的median. 如何减少数据量的传输。

    TO LEARN, 设计题

 

248. You have an array like ar[]= {1,3,2,4,5,4,2}. You need to create another array ar_low[] such that ar_low[i] = number of elements lower than or equal to ar[i] in ar[i+1:n-1].
So the output of above should be {0,2,1,2,2,1,0} Time complexity : O(n) use of extra space allowed.

等价于前面一个求逆序对的题的吧,感觉不可能做到O(N),

 

249. 给一串数字(比如说1,4,10,22,30,表示4个区间:[1,4],(4,10],(10,22],(22,30])。现在给很多个数字,要设计一个快速算法,能用最快的速度告诉那些数字分别落在哪个bucket那里。比如说前面这个例子输入double数13,算法返回string: “(10,22]”;输入double[]序列13,8,25,返回string[] “(10,22]” “(4,10]” “(22,30]”

    TO LEARN, INTERVAL TREE?

 

250. Given Numerator and Denominator. After division you might get a recurring decimal points float as the answer. You need to identify the recurring part? For example 23.34563456 … return 3456

    CODE

 

251. A positive integer is said to be square free if it is divisible by no perfect square larger than 1. For example, the first few squarefree numbers are {1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, …}. Find the nth smallest squarefree number. Note n can be very large such as 1M.

先找出sqrt(n)以内的所有质数。。再想想

 

252.判断一个string是否是某个pattern的周期循环

    TO LEARN,后缀树?

 

253. 在一个N Dimensional 的正方形里面,Assume the top right point is (n,n,…. n) and bottom left point is (0, 0, 0…. 0), given any point in the cube, find all the paths inside the cube to the (n, n,…n) around it, change its value to 1. Otherwise, mark its value to 0 (cannot recall exactly anymore). how to do it. Given a very big such matrix and n computer, how to do it efficiently. Assume each computer has very limited memory, how to do it.

    TO LEARN

 

254. 给个数组,没排序,已知数组中每个元素距离排序以后的位置最多是k,让你给这个数组排序

    TO LEARN, Shell排序?

 

255.两个线段数组,求common区间 A[1,5][10,15]B [3,12] return [3,5],[10,12]

TO LEARN, Interval tree?

256. minimum window cover

    TO LEARN

 

257. Given a sorted array of n integers, pick up k elements so that the minimal difference between consecutive elements is maximal (that is choose the elements to maximize the quantity min(a[i+1] – a[i]))


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值