算法类面试题-6

来源:互联网 发布:创业软件经营状态 编辑:程序博客网 时间:2024/06/16 06:53

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 a way that the sum does not include any two consecutive Fibonacci numbers.

 

from: http://www.mianwww.com/html/2011/10/11338.html

原创粉丝点击